我创建了以下查询。
SELECT users.id, users.email, aucs.winner_id, aucs.beginner, accounts.user_id
FROM users, aucs, accounts
WHERE users.id
IN (
aucs.winner_id
)
AND users.id NOT
IN (
accounts.user_id
)
AND aucs.beginner = '1'
我想选择用户电子邮件地址,该地址仅在列wins_id的aucs表中出现一次,其中aucs.beginner也应该等于'1',并且该帐户表中的用户ID不可用。我写了上面放置的查询不起作用。
答案 0 :(得分:1)
SELECT DISTINCT
users.id,
users.email,
aucs.winner_id,
aucs.beginner,
accounts.user_id
FROM users
INNER JOIN aucs
ON users.id = aucs.winner_id
LEFT JOIN accounts
ON aucs.winner_id = accounts.user_id AND
aucs.beginner = '1'
WHERE accounts.user_id IS NULL
要进一步了解联接,请访问以下链接:
答案 1 :(得分:1)
这是我的头脑,您不需要accounts.user_id,因为您希望获得帐户中不存在user_id的记录。
SELECT users.id as users_id, users.email, aucs.winner_id, aucs.beginner
FROM users
LEFT JOIN aucs
ON aucs.winner_id = users.id
LEFT JOIN accounts
ON accounts.user_id = users.id
WHERE aucs.beginner = '1' AND accounts.id IS NULL
GROUP BY users.id
HAVING COUNT(aucs.winner_id) = 1