我有以下选择声明
SELECT
a.firstname name,
m.date time
FROM
account a
LEFT JOIN memberships m ON a.id = m.account_id
WHERE
a.is_active = 1
我正在尝试检查并查看用户是否具有活动成员资格(可能有多个,但我需要一个作为证据)如果是,那么设置变量true或id或其他内容,如果没有那么false(或null或其他)
这是我到目前为止所拥有的
SELECT
a.firstname name,
p.date time
m.id active_membership
FROM
account a
LEFT JOIN profile p ON a.id = p.account_id
WHERE
a.id IN (SELECT DISTINCT account_id FROM memberships WHERE active = 1)
AND
a.is_active = 1
所以我想要得到的结果 name,time,active_membership foo,10:00,null 酒吧,14:00,223(身份证或任何事情)
我得到了一切正在接受的部分......
答案 0 :(得分:2)
您需要另一个LEFT JOIN
。
SELECT
a.firstname name,
p.date time,
IF(m.account_id IS NULL, "No", "Yes") active_membership
FROM account a
LEFT JOIN profile p ON a.id = p.account_id
LEFT JOIN (SELECT DISTINCT account_id
FROM memberships
WHERE active = 1) m ON a.id = m.account_id
WHERE a.is_active = 1