Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 从两个没有记录的表中进行选择_Mysql_Sql_Database - Fatal编程技术网

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

Mysql 从两个没有记录的表中进行选择,mysql,sql,database,Mysql,Sql,Database,我有两张桌子: 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 ----

我有两张桌子:

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_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')

-根据下面的评论更新。此更新将返回T2中的所有记录,即使T1中不存在,但仅当T2.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'
-这应该是你想要的。它将为您提供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'
从我的头顶创造,希望它能工作


如果您可以发布您编写的查询,也许他们可以告诉我们为什么连接不起作用,因为它应该起作用。

我不知道我是否理解正确,但请尝试让我知道:

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

请提供相关且最低限度的样本数据,说明您的要求和预期输出。Ref:一个问题:如果t2上存在状态为某物的行和状态为!=这辆车的ID是否应该在预期输出上?尝试不存在。我已经尝试过了。。。那么,这些尝试中的一个在哪里呢???我编辑了我的广告,因为评论部分的代码不清楚。与我的类似,但我认为他说没有状态是什么?或者我误读了他的意思asking@Brad我认为作者不是100%清楚。我理解他的意思,因为他要的是没有状态的记录。状态栏应该在t2中。
SELECT DISTINCT t1.ID_car FROM t2  INNER JOIN t1 ON t2.ID_car = t1.ID_car WHERE t2.status != 'something'