Jump to content

MySQL "IN" command


Julian

Recommended Posts

Hi guys... I'm having this situation:

SELECT * FROM pages WHERE status = '1' AND '$colname_page' IN (id_page) ORDER BY orden ASC

 

`id_page` stored data is like (1,5,7,9).  I need to "explode" the field and obtain only "$colname_page" in my results.  Is there any way to do that?  Thanks.

 

e.g.  $colname_page = 5 (I obtain only the records that have 5 on the `id_page` field.)

 

Link to comment
https://forums.phpfreaks.com/topic/156791-mysql-in-command/
Share on other sites

You should never store comma separated values in one record.  You should have a separate table to handle this.  That way you don't run into this problem, and your database will be a little more normalized.

 

Like Ken mentioned, there is no equivalent of explode in PHP in MySQL, AFAIK.

 

You may be able to use REGEXP and match on 5, but I don't this would be the best approach.  (Note: this does not take into account spacing)

 

AND id_page REGEXP '(^5,|,5,|,5$)'

Link to comment
https://forums.phpfreaks.com/topic/156791-mysql-in-command/#findComment-825847
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.