从两个没有记录的表中选择

pprl5pva  于 2021-06-18  发布在  Mysql
关注(0)|答案(4)|浏览(301)

我有两张table:

t1 contain ID_car (unique), name 
t2 contain ID_car(from t1), status (many status records on same ID_car)

我需要以下结果:

All ID_car FROM t1 WITHOUT status = something

我已经尝试过内部,左,右连接,但没有工作。我该怎么做?非常感谢你的帮助!
更多详细信息:

t1
------------
ID_car      name
------------------
1           Toyota
2           Honda
3           Mazda
4           Ford

t2
-----------------
ID_car      status
1           ok
1           not_ok
2           ok
4           not_ok

id\u car 3 din在t2中没有任何记录,但我想显示结果
我需要以下结果(t1的所有车辆没有车辆状态not\ U ok):

the expected result
-----------------
ID_car      status
2           ok
3

更新2终于解决了!谢谢你的帮助!这对我很有用:

SELECT * FROM t1 
WHERE t1.ID_auto NOT IN 
(SELECT DISTINCT t1.ID_auto FROM t1, t2 WHERE t1.ID_auto = t2.ID_auto AND t2.category='not_ok')
yi0zb3m4

yi0zb3m41#

我不知道我是否理解正确,但请告诉我:

SELECT DISTINCT t1.ID_car FROM t2  INNER JOIN t1 ON t2.ID_car = t1.ID_car WHERE t2.status != 'something'
7xllpg7q

7xllpg7q2#

SELECT ID_car
   FROM t1
   LEFT JOIN t2
     ON t1.ID_car=t2.ID_car
  WHERE t2.status=something
qxgroojn

qxgroojn3#

--根据下面的评论更新。此更新将返回t2中的所有记录(即使t1中不存在),但仅当t2.status!=某物
--这应该是你想要的。它将提供t1中的所有记录,以及t2中的任何数据(但不要求在该表中),其中t1.status不是什么

SELECT *
FROM t1 
LEFT JOIN t2 ON T1.ID = T2.ID 
-- did the logic on the JOIN here instead of in where clause, because doing in where clause would force records to appear in t2 table (basically converting it to an inner join) doing it in the join itself does not cause this to happen
AND T2.Status != 'something'
dvtswwa3

dvtswwa34#

SELECT ID_car
FROM t1
  LEFT JOIN
     t2 on t1.ID_car=t2.ID_car
WHERE NOT t2.status='something'

从我的头顶创造,希望它能工作!
也许如果你能发布你写的查询,也许他们能告诉我们为什么连接不起作用,因为它应该起作用。

相关问题