[英]Same column name from two tables satisfying two conditions
我需要一些關於 SQL 的快速幫助。 我敢肯定,這對大多數人來說都是基本的。
我想 select orderId
在兩個表中合並滿足狀態 = 1。
請在此處找到兩個表 tb1 和 tb2 的示例:
tb1
orderId status
---------------
001 0
003 1
005 1
007 1
...
tb2
orderId status
----------------
002 1
008 1
004 0
您可以使用UNION ALL
。
您的查詢將是:
SELECT *
FROM (
SELECT *
FROM tb1
UNION ALL
SELECT *
FROM tb2
) a
WHERE STATUS = 1
使用此查詢:
SELECT
tb1.OrderId,
tb1.Status
FROM
tb1
WHERE
tb1.status = 1
UNION
SELECT
tb2.OrderId,
tb2.Status
FROM
tb2
WHERE
tb2.status = 1;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.