gpt4 book ai didi

mysql union 在重复的情况下优先记录来自选择查询之一的记录

转载 作者:行者123 更新时间:2023-11-29 03:11:21 25 4
gpt4 key购买 nike

这是我在真实示例中的查询:

     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

如果出现重复,我想优先考虑来自第一个选择查询的记录。

如何实现?

最佳答案

只需为每个值添加一个文字值,然后按它排序。

SELECT * 
FROM ((SELECT deal.dealid,
deal.titleen,
deal.capnum,
deal.imagelink,
SUM(buy.quantity) AS quantity,
'A' sortby
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,
'B' sortby
FROM deal
WHERE deal.enddate < Curdate()))a
ORDER BY dealid,
sortby,
titleen,
capnum,
imagelink,
quantity ASC

关于mysql union 在重复的情况下优先记录来自选择查询之一的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7823539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com