[英]How to compare two column from different table and differentiate in mysql?
我正在比較具有相同列的兩個表,只是方法有所不同。
例如
表格1
id roll no
1 123456 same-ignore
2 123457 different
3 123458 same-ignore
4 123879 different
5 123888 same-ignore
表2
id roll no
1 123456 same-ignore
2 123467 different
3 123458 same-ignore
4 123852 different
5 123888 same-ignore
我也遵循以下示例,但未實現結果。
SQL如何比較兩個不同表中的兩個列
比較兩個不同表中的列
比較兩個不同表中的兩列?
比較兩個不同表中的列
我得到這個查詢的第一個輸出。
select distinct roll no from table2 where rollno not in (select roll no from table1)
我無法對第二個輸出執行相同的操作,這意味着無法將table1與table2區分(它返回空行)
我該如何實現?
輸出1 :(不在表1中)
123467
123852
輸出2 :(不在table2中)
123457
123879
對輸出1使用左右連接
select t2.roll from table1 t1
right
join table2 t2 on t1.roll=t2.roll
where t1.roll is null
用於輸出2
select t1.roll from table1 t1
left
join table2 t2 on t1.roll=t2.roll
where t2.roll is null
如果您想讓table1
中的卷不在table2
,則可以這樣做
SELECT roll FROM table1
WHERE roll NOT IN
(
SELECT DISTINCT roll FROM table2
);
UNION
SELECT roll FROM table2
WHERE roll NOT IN
(
SELECT DISTINCT roll FROM table1
);
相反,如果您正在尋找行之間不匹配的滾動對(第一對第一對,第二對第二對……),則可以使用簡單的JOIN
:
SELECT t1.roll
FROM table1 t1 JOIN table2 t2 ON t1.id == t2.id
WHERE t1.roll <> t2.roll
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.