我有两个表:宝石和宝石细节是左连接。我正在尝试将左联接限制为GEMS表中的10条记录。还有另外两个加入的表(GemReplay和USERS),但它们不会导致问题。以下操作不起作用:
SELECT
gems.gemid,
gems.title,
r.tot,
gemdetail.filename
FROM
(
(
gems
LEFT JOIN(
SELECT
gemid,
COUNT(*) AS tot
FROM
gemreply
GROUP BY
gemid
) AS r
ON
gems.gemid = r.gemid
)
LEFT JOIN gemdetail ON gems.gemid = gemdetail.gemid
)
LEFT JOIN users ON gems.userid = users.userid
WHERE
gems.grade = '7'
ORDER BY
gems.gemid
LIMIT 0, 10;
这将返回的总行数限制为10,但是由于每个gem都有多个详细记录,所以我只剩下不到10个gem记录。我看过每一篇“极限”的帖子,但没有发现这种情况。
此版本的MySQL尚不支持‘Limit&IN/All/Any/Some子查询’。
4条答案
按热度按时间nbnkbykc1#
诸如此类的事
bz4sfanl2#
试试这个:
这应该行得通。
jk9hmnmh3#
WITH LAST_ORDERS AS(SELECT*FROM ORDERS WHERE ID IN(SELECT MAX(ID)FOR ORDERS GROUP BY CUSTOMER_ID))SELECT Customers.id,stomers.first_name,Customers.last_name,last_orders.order_date,last_orders.Order_Status from Customers Join Last_Orders on Customers.id=last_orders.Customer_id Order by Customer_id;
ukdjmx9f4#
十年后,我可以在一对多
JOIN
的左侧表上的嵌套查询中LIMIT
记录,其中左侧表将返回多条记录--即使连接的右侧表上的字段上有WHERE
或ORDER BY
子句。