简体   繁体   English

使用MySQL JOINS从两个表中检索记录

[英]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和tbl2,下面的数据如下所示,我想通过合并两个表来获取数据。

tbl1 data: tbl1数据:

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: tbl2数据:

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 . 我想从tbl2获取所有记录,并从tbl1获取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. 我是MySQL语言的初学者。

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 . 该查询选择tbl2所有记录并将它们tbl2STATUS_FLAG = 0tbl1记录。 Duplicates are left out. 重复项被忽略。

Demo here 在这里演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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