0
我想创建此SQL查询条件查询:Select查询
SELECT * FROM `Product` p
ORDER BY (SELECT COUNT(*) FROM `Sale` WHERE `product_id` = p.id) DESC
我不能找到一种方法,与子查询做...
我想创建此SQL查询条件查询:Select查询
SELECT * FROM `Product` p
ORDER BY (SELECT COUNT(*) FROM `Sale` WHERE `product_id` = p.id) DESC
我不能找到一种方法,与子查询做...
Order By
仅适用于一列是存在于select
部分。
所以基本上像这样将工作:
SELECT Group, COUNT(*) as total
FROM table
GROUP BY Group
ORDER BY total DESC
因此,对于你的情况下,你可以这样做以下:
SELECT *, (SELECT COUNT(*) FROM `Sale` WHERE `product_id` = p.id) as total FROM `Product` p
ORDER BY total DESC
注:这是完全未经测试。
尝试的隐式连接:
select *
from (
select
p.*,
(select count(*) from sale s where s.product_id = p.id) as cnt
from product p
)a
order by cnt desc
谢谢,你有一个想法如何使用此SQL查询创建条件查询吗? – skamlet 2013-02-26 22:24:10
看看这些文档是否有帮助:http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html http://www.mkyong.com/hibernate/hibernate-criteria-examples / – Amar 2013-02-26 22:29:44