SQL语句如下:
SELECT A . * , B.num
FROM
表一 A,
(SELECT product_id, count( product_id ) num
FROM 表二
GROUP BY product_id
)B
WHERE A.product_id = B.product_id
AND A.status =1
ORDER BY B.num DESC
其实际上就是把GROUP BY 的结果当作一个新表来使用而已,不知道查询效率如何,或者有没有更好的办法来实现?
另一种实现方法:
SELECT A. * , B.num
FROM 表一 A
INNER JOIN (
SELECT product_id, count( product_id ) num
FROM 表二
GROUP BY product_id
)B
ON A.product_id = B.product_id
AND A.status =1
ORDER BY B.num DESC