简体   繁体   中英

Retrieve records from two tables using MySQL JOINS

I have two tables tbl1 and tbl2 in which data is shown below, I want to fetch the data by combining two tables.

tbl1 data:

SLNO    CALENDAR_RNO    PLNDHRS     STATUS_FLAG
30      64              0.78        0
30      54              0.78        1
30      55              0.78        2
30      70              0.29        0

tbl2 data:

SLNO    CALENDAR_RNO    PLNDHRS
30      53              0.20
30      54              0.10
30      55              0.70
30      56              0.30
30      58              0.18
30      60              0.70
30      62              0.50
30      66              0.70
30      68              0.90

I want to get all the records from tbl2 and get the records from tbl1 which are STATUS_FLAG = 0 .

I tried this query but I am not getting exact

SELECT SLNO,CALENDAR_RNO,PLNDHRS 
FROM tbl1 INNER JOIN tbl2 ON tbl1.SLNO = tbl2.SLNO 
WHERE tbl2.STATUS_FLAG = 0;`

But i need the table as given below:

SLNO    CALENDAR_RNO    PLNDHRS
30      53              0.20
30      54              0.10
30      55              0.70
30      56              0.30
30      58              0.18
30      60              0.70
30      62              0.50
30      64              0.78
30      66              0.70
30      68              0.90
30      70              0.29

Help me out from this problem. I am beginner to MySQL Language.

It seems you just want to union the two tables:

SELECT SLNO, CALENDAR_RNO, PLNDHRS 
FROM tbl1
WHERE STATUS_FLAG = 0

UNION

SELECT SLNO, CALENDAR_RNO, PLNDHRS 
FROM tbl2

The query selects all records of tbl2 and unions them will records of tbl1 having STATUS_FLAG = 0 . Duplicates are left out.

Demo here

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