gpt4 book ai didi

mysql - 如何从两个表中选择不同的行

转载 作者:行者123 更新时间:2023-11-30 23:15:07 26 4
gpt4 key购买 nike

我正在尝试从具有不同字段的 2 个表中随机获取 3 个不同的行

SELECT * FROM `models` JOIN banners WHERE `recomended` = '1' ORDER BY RAND() LIMIT 0,3

我想要这样的结果:

first row - table1.id table1.name table1.points
second row - table2.id table2.group table.2points
third row - table1.id table1.name table1.points

这可能吗?

最佳答案

这是获得所需内容的一种方法:

select id, name, points
from ((select id, name, points, 1 as which
from table1
order by rand()
limit 1
) union all
(select id, group, points, 2 as which
from table2
order by rand()
limit 1
) union all
(select id, name, points, 3 as which
from table1
order by rand()
limit 1
)
) t
order by which;

关于mysql - 如何从两个表中选择不同的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18244988/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com