我有 2 个表,分别命名为表 1、表 2。 table1 列是 t1_id、t1_name、t1_status。 table2 列为 t1_id、t2_id、t2_name、t2_status。 当我在前端执行一些操作时,t1_id 将被插入到 table2 中。 我需要的是 table1 中未分配或插入 table2 中的 t1_id(s)。
我试过这个查询:
SELECT t1.t1_id, t1.t1_name
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON t1.t1_id != t2.t1_id
查询的问题是当所有的 t1_id(s) 都被插入到 table2 中时,所有的 t1_id(s) 又被显示出来了。 如何解决这个问题? (我是 sql 新手,所以请不要考虑我的错误。)
最佳答案
如果我理解你的问题,这一定是你需要的查询:
SELECT t1.t1_id, t1.t1_name
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON t1.t1_id = t2.t1_id
where t2.t1_id is null
关于mysql - 从两张表中取出数据sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31958986/