简体   繁体   English

如何在 JOINED 表的 SQL UNION 中 ORDER BY 未在结果中显示有序列

[英]How to ORDER BY in SQL UNION of JOINED tables not showing ordered columns in result

I'm trying to get the table where two results of joins will be unioned and ordered by several columns.我试图得到一个表,其中两个连接结果将被联合并按几列排序。 I've found that columns that are ordered have to be mentioned in each select of union.我发现在每个联合选择中都必须提到有序的列。 But I don't want ordered columns appear in result.但我不希望结果中出现有序列。 Is it possible?是否可以? Maybe there are another ways of getting the result that I want.也许还有其他方法可以获得我想要的结果。

Here is my query:这是我的查询:

SELECT coupon_id,
       coupon_name,
       real_price,
       discount_price,
       difference_percent,
       discount_percent,
       payment_price,
       photo,
       company_id,
       company_name
FROM
  (SELECT cpn.id AS coupon_id,
          cpn.name_ru AS coupon_name,
          cpn.real_price,
          cpn.discount_price,
          cpn.difference_percent,
          cpn.discount_percent,
          cpn.payment_price,
          cp.photo,
          com.id company_id,
          com.name_ru AS company_name
   FROM coupon cpn
   LEFT JOIN coupon_photo cp ON cpn.id = cp.coupon_id
   LEFT JOIN company com ON cpn.company_id = com.id
   WHERE cpn.id NOT IN
       (SELECT coupon_id AS id
        FROM transaction
        WHERE Date_add(created_time, interval 10 MINUTE) > Now()
          AND status = 'pending')
     AND cpn.exp_date > Now()
     AND cpn.valid_date > Now()
     AND cpn.available > 0
   UNION SELECT cpn.id AS coupon_id,
                cpn.name_ru AS coupon_name,
                cpn.real_price,
                cpn.discount_price,
                cpn.difference_percent,
                cpn.discount_percent,
                cpn.payment_price,
                cp.photo,
                com.id company_id,
                com.name_ru AS company_name
   FROM coupon cpn
   LEFT JOIN coupon_photo cp ON cpn.id = cp.coupon_id
   LEFT JOIN company com ON cpn.company_id = com.id
   LEFT JOIN transaction trans ON cpn.id = trans.coupon_id
   WHERE cpn.exp_date > Now()
     AND cpn.valid_date > Now()
     AND Date_add(trans.created_time, interval 10 MINUTE) > NOW()
     AND trans.status = 'pending'
     AND (cpn.available - trans.amount) > 0 GROUP  BY cpn.id) results
ORDER BY cpn.advertised DESC,
         cpn.sold DESC,
         cpn.views DESC

Your results subquery does not contain the column you are trying to order by .您的results子查询不包含您尝试order by的列。 You need to add them to both parts of the union.您需要将它们添加到联合的两个部分。 You do not have to add them to your select, but they need to be present in the subquery so you can use them in your ORDER BY .您不必将它们添加到您的选择中,但它们需要出现在子查询中,以便您可以在ORDER BY使用它们。

SELECT coupon_id,
       coupon_name,
       real_price,
       discount_price,
       difference_percent,
       discount_percent,
       payment_price,
       photo,
       company_id,
       company_name
FROM
  (SELECT cpn.id AS coupon_id,
          cpn.name_ru AS coupon_name,
          cpn.real_price,
          cpn.discount_price,
          cpn.difference_percent,
          cpn.discount_percent,
          cpn.payment_price,
          cp.photo,
          cpn.advertised,
          cpn.sold,
          cpn.views,
          com.id company_id,
          com.name_ru AS company_name
   FROM coupon cpn
   LEFT JOIN coupon_photo cp ON cpn.id = cp.coupon_id
   LEFT JOIN company com ON cpn.company_id = com.id
   WHERE cpn.id NOT IN
       (SELECT coupon_id AS id
        FROM transaction
        WHERE Date_add(created_time, interval 10 MINUTE) > Now()
          AND status = 'pending')
     AND cpn.exp_date > Now()
     AND cpn.valid_date > Now()
     AND cpn.available > 0
   UNION SELECT cpn.id AS coupon_id,
                cpn.name_ru AS coupon_name,
                cpn.real_price,
                cpn.discount_price,
                cpn.difference_percent,
                cpn.discount_percent,
                cpn.payment_price,
                cp.photo,
                cpn.advertised,
                cpn.sold,
                cpn.views,
                com.id company_id,
                com.name_ru AS company_name
   FROM coupon cpn
   LEFT JOIN coupon_photo cp ON cpn.id = cp.coupon_id
   LEFT JOIN company com ON cpn.company_id = com.id
   LEFT JOIN transaction trans ON cpn.id = trans.coupon_id
   WHERE cpn.exp_date > Now()
     AND cpn.valid_date > Now()
     AND Date_add(trans.created_time, interval 10 MINUTE) > NOW()
     AND trans.status = 'pending'
     AND (cpn.available - trans.amount) > 0 GROUP  BY cpn.id) results
ORDER BY advertised DESC,
         sold DESC,
         views DESC

As Strawberry has pointed out, it is not exactly best practice o use GROUP BY when you are not aggregating anything.正如 Strawberry 指出的那样,当您不聚合任何内容时,使用GROUP BY并不是最佳实践。 In this case it can lead to unexpected results as MySQL uses ANY_VALUE().在这种情况下,它可能会导致意外结果,因为 MySQL 使用 ANY_VALUE()。 It returns the value of some row in the GROUP BY group.它返回 GROUP BY 组中某行的值。 Which row it returns is indeterminate.它返回哪一行是不确定的。 That means it's entirely up to the MySQL server.这意味着它完全取决于 MySQL 服务器。

I assume you were using it to avoid duplicate rows.我假设您使用它来避免重复行。 You should find another de-duplication method.您应该找到另一种重复数据删除方法。

Furthermore, doing a LEFT JOIN but then filtering the elements on the right side of the JOIN is the same as doing an INNER JOIN.此外,执行 LEFT JOIN 然后过滤 JOIN 右侧的元素与执行 INNER JOIN 相同。 Just make sure this is consistent with what you are trying to achieve.只要确保这与您要实现的目标一致即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM