1

我正在尝试从具有不同字段的 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

那有可能吗?

4

1 回答 1

0

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

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;
于 2013-08-15T01:46:56.043 回答