Jump to content

Comparing two comma separated results


watsmyname

Recommended Posts

Hello,

 

I have two queries that extracts result from two tables.  First query fetches a field that has comma separated values. And second query as well fetches a field from another table that has comma separated values. For example

 

first_result_field: 12,34,56,19,5,17,28

second_result_field: 5,12,6,9,13,78,45,11,46

 

now my problem is to get common id(or ids) from these two results. Like we have 5 and 12 common in both results in above example. HOw can i get this via mysql query?

 

Thanks

watsmyname

 

Link to comment
Share on other sites

Firstly, why are you storing comma separated data in a database? This would be allot simpler if your data was normalised.

 

Anyway, implode your string into arrays then use array_interest.

 

thanks for the reply, I have used comma separated because lets say I have a products that fall under 100's of categories and subcategories and again there will be millions of products. Storing each  on single row the size of the database will be bulk which i think ultimately reduces the query execution time. And in regard to the solution you offered, in php i know it can be done using array_intersect function, but i have to achieve this in Mysql.

 

watsmyname

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.