UNION ALL - > ORDER BY没有按预期工作......?

时间:2016-12-03 13:22:16

标签: mysql sql

我有以下SQL语句:

  (SELECT animation_id,
          animation_name,
          animation_group
   FROM animations
   WHERE animation_id = '45')
UNION ALL
  (SELECT animation_id,
          animation_name,
          animation_group
   FROM animations
   WHERE animation_id <> '45'
     AND active = TRUE
   ORDER BY animation_group,
            animation_name)

表格如下:

Table

排序不正常。我希望数据按“animation_group”列排序,但结果在“animation_id”之后排序。 查看屏幕截图,其中显示了输出:

Sorting

如果相关:它是MySQL。该声明正在通过PHP调用。

SQL语句是否正确?

谢谢!

修改 我的请求的基础或让我们说我使用UNION的原因如下:

第一次选择的结果应该始终是第一行。 第二个选择的结果应按顺序排序。因此,我不想订购整个结果。

5 个答案:

答案 0 :(得分:1)

首先,没有group by而问题是order by。其次,不知道为什么你需要UNION。您可以使用OR条件

将查询更改为如下所示
SELECT animation_id, animation_name, animation_group
FROM animations
WHERE animation_id = '45'
OR (animation_id <> '45' and active = true)
ORDER BY animation_group, animation_name;

答案 1 :(得分:0)

试试这个

SELECT * FROM (
  SELECT animation_id, animation_name, animation_group
  FROM animations
  WHERE animation_id = '45'
UNION ALL 
  SELECT animation_id, animation_name, animation_group
  FROM animations
  WHERE animation_id <> '45' and active = true
) as tmp_table
ORDER BY animation_group, animation_name;

答案 2 :(得分:0)

请勿在括号内附上订单。 这样做,仅对第二个查询执行顺序,而不是结果。此外,UNION可以自由重新排序结果。在一个或多个UNION之后的订单适用于工会的结果,但前提是您不使用括号更改其范围。 如果删除括号,您的查询将起作用。

答案 3 :(得分:0)

手动为所选项目添加排序顺序:

SELECT
  animation_id, 
  animation_name, 
  animation_group,
  CASE WHEN animation_id = '45' THEN 1 ELSE 0 END AS is_selected
FROM 
  animations
WHERE 
  animation_id = '45' OR (animation_id <> '45' AND active = true)
ORDER BY 
  is_selected DESC,
  animation_group, 
  animation_name

如果您仍想出于某种原因保留UNION

SELECT 
  animation_id, 
  animation_name, 
  animation_group,
  1 AS is_selected
FROM 
  animations
WHERE 
  animation_id = '45'
UNION ALL -- If animation_id is unique, I can't see why you'd need a UNION ALL here, by the way 
SELECT 
  animation_id, 
  animation_name, 
  animation_group,
  0 AS is_selected
FROM 
  animations
WHERE
  animation_id <> '45' AND 
  active = true
ORDER BY
  is_selected DESC,
  animation_group, 
  animation_name

另外,假设animation_id是一个唯一的整数,1)你不需要UNION ALL,因为无论如何选择的每一行都是唯一的,2)你不需要周围的引号这些值,即只有animation_id = 45才有效,而不是animation_id = '45'

答案 4 :(得分:0)

&#34; UNION ALL - &gt; ORDER BY没有按预期工作&#34; - 事实上,它没有按预期工作,它正在以documented工作:

  

对单个ORDER BY语句使用SELECT表示行在最终结果中的显示顺序,因为默认情况下UNION会生成一组无序行。因此,在此上下文中使用ORDER BY通常与LIMIT结合使用,因此它用于确定要为SELECT检索的所选行的子集,即使它不一定会影响最终UNION结果中这些行的顺序。如果ORDER BYLIMIT出现SELECT,则会对其进行优化,因为它无论如何都无效。

     

要使用ORDER BYLIMIT子句对整个UNION结果进行排序或限制,请为各个SELECT语句添加括号并放置ORDER BY或{ {1}}在最后一个之后。

根据文档建议,您的查询应为:

LIMIT

此外,您的查询会选择包含( SELECT animation_id, animation_name, animation_group FROM animations WHERE animation_id = '45' ) UNION ALL ( SELECT animation_id, animation_name, animation_group FROM animations WHERE animation_id <> '45' and active = true ) ORDER BY animation_group, animation_name animation_id = 45的记录,因此可以将其写为:

active = true
相关问题