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
https://forums.phpfreaks.com/topic/174822-mysql-count-multiple-columns/
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.