mysql - 从两张表中取出数据sql

标签 mysql sql-server sql-optimization

我有 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/

相关文章:

mysql - 我需要一些帮助来优化查询

sql - 如何对递归查询生成的板进行排序

sql-server - 使用查询或存储过程延迟 SQL Server 2000 查询直到处理器利用率低于 50%?

SQLite数据库优化

php - 方法不返回任何内容

c# - 编辑自定义 SSIS PipelinComponent 抛出错误 "value of ' null' is not valid for 'stream' "

MySQL:在这个例子中使用例程来查找匹配不是应该更快吗?

mysql - 从另一个表mysql镜像一行

mysql - 如何将 "WITH"子句 MS SQL 转换为 MySQL

mysql - 将太大而无法放入内存的 mysql 表下载到 csv 文件中(使用 WorkBench)