我需要检索用户的随机ID,该用户在另一个表中具有非空的头像字段
SELECT userid FROM table1 WHERE EXISTS (SELECT 'avatar' FROM table2 WHERE avatar IS NOT NULL) ORDER BY RAND() LIMIT 1;
此查询返回的结果不正确,同时显示我没有头像的人
答案 0 :(得分:0)
我假设一个表结构如下:
table1 {
userid primary key
[...]
}
table2 {
[...]
userid foreign key
avatar
}
以下SQL应该有效:
SELECT userid FROM table1 a
LEFT JOIN table2 b ON a.userid = b.userid
WHERE avatar IS NOT NULL
ORDER BY RAND() LIMIT 1