无法正确获得 SQL 的预期联合输出

Cannot correctly get expected Union output for SQL

烦恼:

没有正确排序,也没有按预期布置 code/or 使用的约定。

问题:

此处需要使用 Union 运算符,为此,找到所用代码第三行中指定的 vendor_id,并且数量小于最终指定的数量代码行。代码需要排序。

使用的代码:

SELECT job_id, po_id, 'Vendor ' || vendor_id as 'Reason'
FROM pos
WHERE vendor_id IS 'SOS'
UNION
SELECT job_id, po_id, 'Quantity < ' || quantity as 'Reason'
FROM pos
WHERE quantity < 10;

想要得到:

job_id      po_id       Reason
----------  ----------  ------------
002         AAA         Quantiy < 10
004         CCC         Quantiy < 10
004         CCC         Vendor SOS
005         EEE         Vendor SOS
006         GGG         Quantiy < 10

收到(来自 CodeRunner):

Runtime error
Program does not use the expected ORDER BY clause or is badly laid out.

See database schema for more details (Primary Keys are in bold).

使用了未知的 DBMS。表现得像 PostgreSQL 而不是 mysql

要按 job_id 排序,然后按 po_id 排序,只需在查询末尾添加 ORDER BY 子句。默认排序顺序ASC(升序),如果要降序排序可以加上DESC

SELECT
    job_id, 
    po_id, 
    'Vendor ' || vendor_id as 'Reason'
FROM pos
WHERE vendor_id = 'SOS'

UNION

SELECT 
    job_id,
    po_id, 
    'Quantity < ' || quantity as 'Reason'
FROM pos
WHERE quantity < 10

ORDER BY 
    job_id,
    po_id;