mysql - 在一个查询中将表连接在一起

时间:2010-12-26 12:34:28

标签: mysql

快乐的圣诞节。任何人都可以帮我提出这个特定情况的查询:〜我需要生成一个活动列表,显示为每个活动注册的每个性别的子女数量。什么是查询? 谢谢。 表格结构如下:有活动,儿童,儿童活动表。

ACTIVITY TABLE
( activity_id[Primary Key], 
  Activity)

CHILD TABLE
( child_id[Primary Key], 
  child_name, 
  child_gender, 
  child_dob)

CHILD ACTIVITY TABLE
( activity_id[Composite Primary Key], 
  child_id[Composite Primary Key]
  Activity/Day Of Week)

1 个答案:

答案 0 :(得分:3)

SELECT a.activity_id, a.Activity, c.child_gender, COUNT(*)
FROM ACTIVITY a
 JOIN [CHILD ACTIVITY] ca ON a.activity_id = ca.activity_id 
 JOIN CHILD c ON ca.child_id = c.child_id
GROUP BY a.activity_id, a.Activity, c.child_gender