从table1中选择table1中条件特定元素不存在于table2中

时间:2016-01-13 17:42:46

标签: mysql conditional-statements where

words包含列:word_id word

我的查询打印为:

SELECT word FROM words WHERE condition1 AND condition2 ORDER BY RAND() LIMIT 5000

solved包含列:user_id word_id

我希望获取5000个随机行,条件是solved

user = username中找不到任何行

到目前为止,我已尝试过:

SELECT word FROM words w 
WHERE not exists 
(select 1 from solved s where s.word_id = w.word_id 
AND s.user_id = 'username') 
AND condition1 AND condition2 ORDER BY RAND() LIMIT 5000

1 个答案:

答案 0 :(得分:0)

LEFT JOIN用于thar:

SELECT word 
FROM words w 
left join solved s on s.word_id = w.word_id AND s.user_id = 'username'
WHERE w.word_id is null
AND condition1 AND condition2 
ORDER BY RAND() LIMIT 5000