查询在MySQL中搜索

时间:2011-08-09 19:09:35

标签: mysql

我有3张桌子

members (id, name, email)
members_kids (id, member_id, kids_name, kids_age)
member_kids_activity_preferences (id, kid_id, activity_id)

我有一系列活动(4, 10, 12, 14)

我需要做的是从成员中选择不同的电子邮件地址WHOSE孩子从4,10,12,14表中优先选择member_kids_activity_preferences。我如何使用1个查询?

杰森

3 个答案:

答案 0 :(得分:2)

快速拍摄:

SELECT DISTINCT email FROM members m
    JOIN members_kids k ON m.id = k.member_id
    JOIN member_kids_activity_preferences p ON k.id = p.kid_id
  WHERE p.activity_id IN (4,10,12,14)

答案 1 :(得分:2)

SELECT m.name,m.email
FROM members m
   JOIN members_kids k
      ON m.id = k.member_id
   JOIN member_kids_activity_preferences ap
      ON k.id = ap.kid_id
WHERE ap.id IN (4, 10, 12, 14)
GROUP BY m.name,m.email

答案 2 :(得分:0)

SELECT DISTINCT
  m.email
FROM
  members AS m
INNER JOIN
  members_kids AS mk
ON
  m.id = mk.member_id
INNER JOIN
  member_kids_activity_preferences AS mkap
ON
  mk.id = mkap.kid_id
WHERE
  mkap.activity_id IN (4,10,12,14)