mysql union 优先记录来自选择查询之一的记录,以防重复
这是我的查询的一个真实示例:
SELECT *
FROM (
(SELECT Deal.DealID, Deal.TitleEn, Deal.CapNum,Deal.ImageLink,
SUM( Buy.Quantity ) AS Quantity
FROM Buy, Deal
WHERE Buy.DealID = Deal.DealID
AND Buy.IsFinalBuy = '1'
GROUP BY Buy.DealID
HAVING Quantity = Deal.CapNum)
union
(SELECT Deal.DealID, Deal.TitleEn, Deal.CapNum,Deal.ImageLink,NULL AS Quantity
FROM Deal
WHERE Deal.EndDate < CURDATE( )
)
)A
ORDER BY DealID,TitleEn,CapNum,ImageLink,Quantity ASC
我想优先考虑第一个选择查询中的记录,以防出现重复。
怎么办呢?
This is my query in a real example :
SELECT *
FROM (
(SELECT Deal.DealID, Deal.TitleEn, Deal.CapNum,Deal.ImageLink,
SUM( Buy.Quantity ) AS Quantity
FROM Buy, Deal
WHERE Buy.DealID = Deal.DealID
AND Buy.IsFinalBuy = '1'
GROUP BY Buy.DealID
HAVING Quantity = Deal.CapNum)
union
(SELECT Deal.DealID, Deal.TitleEn, Deal.CapNum,Deal.ImageLink,NULL AS Quantity
FROM Deal
WHERE Deal.EndDate < CURDATE( )
)
)A
ORDER BY DealID,TitleEn,CapNum,ImageLink,Quantity ASC
I want to give priority to the record from the first select query in case of duplicates .
How can it be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需为每个值添加一个文字值,然后按其排序即可。
Just add a literal value to each one and then order by it.