php - Storing and selecting multiple ids in database -
i have newsletter component , users can subscribe multiple newsletters. stored in table of subscribers this: 4,8,11 (these id's of newsletters subscriber subscribed to)
to select subscribers of newsletter use:
"select * #__newsl_subscribers newslids '%" . (int) $id."%'";
when want select users receive newsletter id 1, user receives newsletter 11 (or 12, 10 etc..) selected. , that's problem.
is there select-statement this? otherwise have store id's brackets around them [1],[11], etc...
you should avoid solutions several ids stored in 1 field. instead should use foreign keys and, in case n:m relationships, relationship tables. n:m-relationships these a can have multiple b , vice versa.
i following:
table "subscriber": no information newsletters
table "newsletter": no information subscribers
new table "newsletter_subscriber":
field: subscriber_id
field: newsletter_id
both fields foreign keys (references primary key of table).
to select subscribers of specific newsletter, can use statement:
select s.* subscriber s inner join newsletter_subscriber ns on ns.subscriber_id = s.id ns.newsletter_id = <newsletter id>
and vice versa, select newsletters of specific subscriber:
select n.* newsletter n inner join newsletter_subscriber ns on ns.newsletter_id = n.id ns.subscriber_id = <subscriber id>
btw should appologize not answering original question. maybe know database normalization, having other reasons these ids stored in 1 field?
Comments
Post a Comment