【发布时间】:2013-12-10 13:38:38
【问题描述】:
我有这个选择语句:
select A.id,
(select id from B order by rand() limit 1)b1,
(select id from B where not id in(b1) order by rand() limit 1)b2,
(select id from B where not id in(b1,b2) order by rand() limit 1)b3,
(select id from B where not id in(b1,b2,b3) order by rand() limit 1)b4,
(select id from B where not id in(b1,b2,b3,b4) order by rand() limit 1)b5
from A
它不是很快,它不会给我一个错误,但也没有做我想要的。
我想从表 B 中读取 5 个随机 id 并将它们连接到表 A。
到目前为止一切顺利,我从表 B 中得到了 5 个 id 的结果,但是有双打。
即使我有这个应该防止双打的 where 子句,我还是明白了。
例如 A.id:1 有 b1=1, b2=6, b3=1, b4=9, B5=3
如果MySQL因为无法处理该语句而引发错误,我会理解,但什么也没有,所以我认为它应该可以工作,但它没有。
谁有答案?
编辑: 结果看起来像这样(子查询)并不重要: 1:2,7,3,9,6 或像这样(加入): 1:2 1:7 1:3 1:9 1:6
只要每个 A.id 都有不同的 B.id。两个或多个 A.Id 有相同的 B.id 是可以的,但应该是巧合。
仍然是 MySQL 接受查询并给出错误结果的问题。
【问题讨论】:
标签: mysql sql subquery correlated-subquery