我看起来很简单:
SELECT tag_id
from books_tags
WHERE tag_id IN (SELECT tag_id
FROM books_tags
GROUP BY tag_id
ORDER BY COUNT(tag_id) DESC
LIMIT 100)
ORDER BY RAND()
LIMIT 1
我希望从里面的查询中选择一条随机记录。它不起作用,错误说LIMIT和ORDER不能出现在子查询中。
我不想创建临时表,或者在服务器端随机创建。任何想法如何将其合并到wone查询?
答案 0 :(得分:0)
您应该可以使用INNER JOIN
/ LEFT OUTER JOIN
SELECT a.tag_id
from books_tags a
INNER JOIN (SELECT tag_id
FROM books_tags
GROUP BY tag_id
ORDER BY COUNT(tag_id) DESC
LIMIT 100) b ON a.tag_id = b.tag_id
ORDER BY RAND()
LIMIT 1