我想从一个表中选择与另一个表具有不同值的记录。
表 1。
+--------+-------+
| userID | tagID |
+--------+-------+
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
+--------+-------+
表 2。
+---------+-------+
| ChaName | tagID |
+---------+-------+
| Hello | 1 |
| How | 2 |
| Are | 3 |
| You | 4 |
| Today | 5 |
| Guys | 6 |
| ? | 7 |
+---------+-------+
然后应该是
+--------+-------+---------+
| userID | tagID | chaNAME |
+--------+-------+---------+
| 1 | 1 | Hello |
| 1 | 5 | Today |
| 1 | 6 | Guys |
| 1 | 7 | ? |
+--------+-------+---------+
看起来很简单,但我找不到解决的方法。
谢谢大家的回答<3
Unrecognized keyword. (near "not in" at position 92)
最佳答案
要获取输出,您需要使用外部联接
。 左连接
将为您提供左表table2
中的所有记录以及右表table1
中的匹配记录。这意味着 table1
中的记录在 table2
中不存在,您将获得 null
。您可以在 where
子句中使用此属性来仅获取您期望的记录。
此外,您无法决定 table2
中记录的 userid
。如果您想将它们硬编码为 1
,请在 select
子句中使用 select 1 as userid, ...
。
select t2.tagid,t2.chaname
from
table2 t2 left join table1 t1
on t1.tagid=t2.tagid
where t1.tagid is null
关于MYSQL|选择两个表之间的倒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35837262/