Jump to content

MySql Count() Multiple Columns


BioBob

Recommended Posts

Hi!

 

I have a query that I want to count the total number of matches in each row.

 

DB example

 

recipes

  Cereal  recipe_id INT

  Hamburger recipe_id INT

  CHow Mein recipe_id INT

 

ingredients

  recipe_id  (joins to recipe_id in recipes)

  lettuce      ENUM 'y','n'

  tomato      ENUM 'y','n'

  onion        ENUM  'y','n'

  ketchup    ENUM  'y','n'

  mustard    ENUM 'y','n'

 

I've been trying to use SUM, COUNT, HAVING, GROUP BY and every other function I can think of to get this to work.  None of my queries work right so Im great having a brand new query

 

SELECT recipe_id, COUNT(count what?) FROM ingredients WHERE (I build a list elsewhere  ) onion = 'y' OR ketchup = 'y'

 

Then COUNT the number of fields that match.  So if I have onion and ketchup I can make a new results column for COUNT and it gets set to 2, or like if I just have onion match i get a count of 1.  Does that make sense? 

 

result[0] recipe_id value: 43 matches value 2 result[1] recipe_id value 51 value 1

 

Database wasnt set up to use anything other than matches like that so just wondiering how I can accomplish this...

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.