简体   繁体   中英

Same column name from two tables satisfying two conditions

I need some quick help on SQL. This is basic for most I am sure.

I want to select orderId in both tables merged that satisfies status = 1.

Please find example of the two table tb1 and tb2 here:

tb1

orderId  status
---------------
001     0
003     1
005     1
007     1
...

tb2

orderId  status
----------------
002      1
008      1
004      0

You can use UNION ALL .

Your query would be:

SELECT *
FROM (
   SELECT *
   FROM tb1

   UNION ALL

   SELECT *
   FROM tb2
) a
WHERE STATUS = 1

Use this query:

SELECT 
    tb1.OrderId, 
    tb1.Status
FROM
    tb1
WHERE 
    tb1.status = 1

UNION 

SELECT
    tb2.OrderId, 
    tb2.Status
FROM
    tb2
WHERE 
    tb2.status = 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM