Just another little query Problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Just another little query Problem

Post by AGISB »

I got following query

Code: Select all

SELECT sum( b.softdiet = 'TRUE' ) AS soft_cnt, sum( b.mediumdiet = 'TRUE' ) AS medium_cnt, sum( b.harddiet = 'TRUE' ) AS hard_cnt
FROM Recipe_Step a, Recipes b
WHERE a.Recipe_ID = b.Recipe_ID 
AND 'TRUE' IN (b.softdiet, b.mediumdiet, b.harddiet) 
AND (a.Ingred_ID =118 OR a.Ingred_ID =327)
it displays the sum of the diettypes of recipes that have certain ingredients.

That is working fine except one little problem. A recipe which has both ingredients counts double. Is there a way I can work in DISTINCT into above query?

My solution for now would be adding a.Recipe_ID to the Select and Group by it. Then I would just count each entry > 0 as 1 with php. However as always I like to learn more about MYSQL ;)
Post Reply