表A
id | val1 | val2 | val3 | 状态 |
---|---|---|---|---|
1 | 啊啊 | 430 | 40 | 1 |
2 | bb | 760 | 45 | 2 |
3 | ccc | 650 | 90 | 1 |
4 | dd | 680 | 30 | 1 |
5 | 哎呀 | 200 | 20 | 2 |
表B
id | T1_id | TB_1 | TB_2 |
---|---|---|---|
1 | 1 | a | 啊 |
2 | 2 | b | bb |
3 | 2 | c | 抄送 |
4 | 2 | d | dd |
5 | 3 | e | 诶 |
6 | 4 | f | ff |
7 | 5 | g | gg |
8 | 5 | h | hh |
输出为
val1 | val2 | val3 | TB_1 | TB_2 |
---|---|---|---|---|
bb | 760 | 45 | b | bb |
bb | 760 | 45 | c | 抄送 |
bb | 760 | 45 | d | dd |
诶诶 | 200 | 20 | g | gg |
哎呀 | 200 | 20 | h | hh |
如果两个表都有记录则
SELECT A.*, B.*
FROM A
LEFT JOIN B ON A.id = B.T1_id
WHERE A.status = 2;
如果只有表A
SELECT A.*
FROM A
LEFT JOIN B ON A.id = B.T1_id
WHERE A.status = 2;