sql - 无法正确获取 SQL 的预期联合输出
问题描述
问题:
没有正确订购,并且没有按预期布置代码/或使用约定。
问题:
Union
此处需要使用运算符,为此,在使用的代码的第三行中指定了 find ,并且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.
使用了未知的 DBMS。表现得像PostgreSQL
和不像mysql
。
解决方案
job_id
要按follow by排序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;
推荐阅读
- c# - Unity Videoplayer如何暂停和播放
- python - 有什么方法可以让鼠标事件完全忽略 PyQt5 中的窗口?
- javascript - 为自定义按钮使用自定义 CSS (FullCalendar)
- graphql - 何时使用 `graphql` 的 `visit` 功能进入/离开
- flutter - 在颤动闪屏之前删除白屏
- java - 从数组中选择一个随机对象
- spring - 未找到依赖项:openfeign
- html - 将粘性位置与绝对位置一起使用
- javascript - ReactJS - 本地图像未显示
- python - Django Rest Framework:从序列化程序中的视图访问传递的参数