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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.