开发者

Query for Searching in MySQL

I have 3 tables

members (id, name, email)
members_kids (id, member_id, kids_name, kids_age)
member_kids_activity_preferences (id, kid_id, activity_id)
开发者_Go百科

I have an array of activities (4, 10, 12, 14)

What I need to do is select distinct email addresses from members WHOSE kids have a preference for 4,10,12,14 from the member_kids_activity_preferences table. How do i do it using 1 query ?

Jason


Quick shot:

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)


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


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜