如何通过使用 join 2 次或更多次 select 随机 2 行
How to select randomly 2 rows by using join 2 or more times
我有 3 个 table:
News | Categories | News_in_cat
id | id | id
title | name | news_id
| | cat_id
目标是从类别 table 中创建 mySql select 1 个随机类别,然后在 News_in_cat table 中查找 return 2 条随机新闻,来自 selected 类别,来自新闻 table。
SELECT * FROM News_in_cat AS nic
(SELECT * FROM categories ORDER BY RAND() limit 1) AS t
ON t.id = nic.cat_id
INNER JOIN News AS n
ON n.id = nic.news_id
ORDER BY RAND()
LIMIT 2;
我有 3 个 table:
News | Categories | News_in_cat
id | id | id
title | name | news_id
| | cat_id
目标是从类别 table 中创建 mySql select 1 个随机类别,然后在 News_in_cat table 中查找 return 2 条随机新闻,来自 selected 类别,来自新闻 table。
SELECT * FROM News_in_cat AS nic
(SELECT * FROM categories ORDER BY RAND() limit 1) AS t
ON t.id = nic.cat_id
INNER JOIN News AS n
ON n.id = nic.news_id
ORDER BY RAND()
LIMIT 2;