mysql union 优先记录来自选择查询之一的记录,以防重复

发布于 2024-12-10 22:38:34 字数 543 浏览 1 评论 0原文

这是我的查询的一个真实示例:

     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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

徒留西风 2024-12-17 22:38:34

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

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 

Just add a literal value to each one and then order by it.

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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文