tags:

views:

27

answers:

3

Hi Guys!

I have two tables like this.

Table 1 Fields: CId , Name Table 2 Fields: CId , food

I want to get no. of food against each CId with the query "select * from table1"

+1  A: 
SELECT
   food
FROM
   Table2 t2
   JOIN Table1 t1 ON (t2.Cld = t1.Cld)
tandu
I just want to get no. of foods against each CId not the food itselft
EarnWhileLearn
replace ' food' with Count(food)
Emerion
The JOIN would require the existence of food in t2, so the records from t1 which do not have any food in t2 will be ignored from the resultset.
Sabeen Malik
Also if i understand this correctly, the lack of group by would cause to count all rows and not just the ones linked the t1 record.
Sabeen Malik
+1  A: 
select a.name, b.food from table1 a, table1 b where a.cld = b.cld;
Joshua
+1  A: 

Something like that should work:

SELECT
  t1.* , count(t2.food) as foods
FROM
  t1 LEFT JOIN t2 on (t1.Cid = t2.Cid)
GROUP BY
  t2.Cid
Sabeen Malik