帮忙看一下这个sql怎么写才好. g_trade_tradelist 是订单表
g_trade_goodslist 是物品明细表
两个表之间tradeid字段是关联的.
tradelist中tradestatus=5 and printexpress=''之外的单是不查询的.
现在想查询在g_trade_goodslist中出现次数最多的货品(货品列名是'goodsid')在g_trade_tradelist中对应的所有订单(满足tradestatus=5 and printexpress=''两个条件的)
我用的是
select * FROM [wdgj30].[dbo].[G_Trade_GoodsList] where TradeID in(
select TradeID from (
select top 1 goodsid,count(GoodsID)as c from wdgj30.dbo.G_Trade_GoodsList where TradeID in(
select tradeid from wdgj30.dbo.g_trade_tradelist where tradestatus=5 and printexpress=''
)group by GoodsID order by c desc
SELECT *
FROM g_trade_tradelist a
WHERE EXISTS ( SELECT 1
FROM ( SELECT TOP 1
tradeid ,
COUNT(1) c
FROM g_trade_goodslist
GROUP BY tradeid
ORDER BY COUNT(1)
) b
WHERE a.tradeid = b.tradeid )
AND tradestatus = 5
AND printexpress = ''
------解决方案-------------------- 能否給個你想要的結果 ------解决方案-------------------- exists是一个筛选条件。满足的就筛选,不满足就不筛选,所以和你select* from g_trade_tradelist where tradestatus=5 and printexpress=''不一样的 ------解决方案--------------------