[英]query to check if records exists in both or either of 2 columns
我嘗試為您的預期結果創建 select ,但是將為忽略行設置一些已更改的語句:
SELECT col1, col2, done_at, SUM(qty) FROM (
SELECT col1, col2, done_at, qty, 0 AS semiline
FROM test WHERE 1
UNION ALL
SELECT test1.col2 AS col1, test1.col1 AS col2, test1.done_at, test1.qty, 1 AS semiline
FROM test AS test1
JOIN test AS test2 ON test1.col2=test2.col1
WHERE 1 GROUP BY test1.col2, test1.col1, test1.done_at
) AS result WHERE 1
GROUP BY col1, done_at;
+------+------+------------+----------+
| col1 | col2 | done_at | SUM(qty) |
+------+------+------------+----------+
| ABC | XYZ | 2017-12-29 | 2 | <-- this row is bad, i think...
| XYZ | 123 | 2017-12-29 | 3 |
| XYZ | 123 | 2018-10-04 | 7 |
+------+------+------------+----------+
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.