Home > Back-end >  Same column name from two tables satisfying two conditions
Same column name from two tables satisfying two conditions

Time:12-02

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

CodePudding user response:

You can use UNION ALL.

Your query would be:

SELECT *
FROM (
   SELECT *
   FROM tb1

   UNION ALL

   SELECT *
   FROM tb2
) a
WHERE STATUS = 1

CodePudding user response:

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;
  • Related