" MySQL返回一个空结果集

时间:2014-04-05 14:14:39

标签: mysql join random

我有一张桌子(桌子1),我存放了一些人。我需要逐个选择随机并添加到另一个表(table2)。如果添加了一个人,则可以再次选择该人,直到选中所有人为止。这是我的问题:

SELECT * FROM people inner join people_generated on people.id = people_generated.id WHERE people.id != people_generated.id_people ORDER BY RAND() LIMIT 1 

第二个表“people_generated”是空的,所以留下人来生成。为什么结果是空的?

1 个答案:

答案 0 :(得分:0)

INNER JOIN返回两个表中的匹配行,如果一个为空,则结果将始终为空。

您可能想要使用

SELECT * FROM people LEFT JOIN people_generated on people.id = people_generated.id WHERE people_generated.id_IS NULL ORDER BY RAND() LIMIT 1