Jump to content

Recommended Posts

Image a table has the following records

 

Fruits

idnamecolors

1apples1,2

2oranges3

 

Colors

idname

1red

2green

3orange

 

Can I write a query to count how many records have a 2 in the colors field in the Fruits table?

 

I though "SELECT COUNT(name) AS amount FROM fruits WHERE colors LIKE '%2%'" would do it, but I don't think LIKE likes numbers.

Link to comment
https://forums.phpfreaks.com/topic/140215-solved-help-with-like-query/
Share on other sites

CSV data doesn't belong in mysql row columns.  You should reorganize your tables just like corbin said in the other thread.  There are string searching functions (http://dev.mysql.com/doc/refman/5.0/en/string-functions.html) but I'm not going to give you an example because they are more trouble than it's worth for this example.

 

If you had the tables setup like suggested before, you could count how many instances of 2 existed and it would tell you happily.

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.