MySQL视图:Union Query失败

时间:2017-11-16 03:01:32

标签: mysql view union

我尝试根据以下查询创建视图,该视图可以作为查询正常工作:

SELECT x_doc_status.Status, sort_order FROM x_doc_status WHERE sort_order IS NOT NULL
UNION  
SELECT "[All]", -2 
UNION  
SELECT "[Open/App] (Trk/Pnd/App)", -1 
UNION 
SELECT "[Open] (Tracking & Pending)", 0
ORDER BY sort_order, STATUS;

但是当我尝试从中创建视图时它失败了。任何建议都将非常感谢!

1 个答案:

答案 0 :(得分:0)

对文字使用单引号

SELECT
      x_doc_status.Status
    , sort_order
FROM x_doc_status
WHERE sort_order IS NOT NULL
UNION
SELECT
      '[All]'
    , -2
UNION
SELECT
      '[Open/App] (Trk/Pnd/App)'
    , -1
UNION
SELECT
      '[Open] (Tracking & Pending)'
    , 0
ORDER BY
      sort_order
    , STATUS
;

我建议你也使用UNION ALL而不是UNION。前者更快,因为它不会尝试删除重复的行。