Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.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 SQL:选择不带';不存在于第二个表中_Mysql_Sql - Fatal编程技术网

Mysql SQL:选择不带';不存在于第二个表中

Mysql SQL:选择不带';不存在于第二个表中,mysql,sql,Mysql,Sql,表1: 表2: +-----------+----------+ | CODE | TYPEID | +-----------+----------+ | 441 | mn014 | | 223 | mn014 | | 224 | mn014 | | 655 | mn089 | | 854 | mn089 | | 449 | mn032 | +-----------+----

表1:

表2:

+-----------+----------+
|    CODE   |  TYPEID  |
+-----------+----------+
| 441       |  mn014   |
| 223       |  mn014   |
| 224       |  mn014   |
| 655       |  mn089   |
| 854       |  mn089   |
| 449       |  mn032   |
+-----------+----------+
我想选择类型ID mn014的所有代码,这些代码要么不存在于第二个表中,要么在RTURNDTE列中不为空,它们存在于表2中的所有实例中

我是这样想的:

+-----------+----------+----------+
| CODE      | TAKENDTE | RTURNDTE |
+-----------+----------+----------+
| 441       | 25/08/14 | 01/01/15 |
| 223       | 25/08/14 | 03/01/15 |
| 223       | 25/08/14 | 01/02/15 |
| 223       | 25/08/14 |   NULL   |
| 655       | 25/08/14 | 07/01/15 |
| 854       | 25/08/14 |   NULL   |
| 449       | 25/08/14 | 06/01/15 |
+-----------+---------------------+

这不起作用,有什么想法吗?

您可以将其表述为
不存在
——使用
表2上的过滤器。您正在查找表1中没有返回日期为
NULL
的表2中记录的记录(如果我理解正确的话)

试试这个:

select t1.*
from table1 t1
where typeid = 'mn014' and
      not exists (select 1
                  from table2 t2
                  where t2.code = t.code and
                        t2.rturndte is null
                 );

工作得很好!非常感谢你
select t1.*
from table1 t1
where typeid = 'mn014' and
      not exists (select 1
                  from table2 t2
                  where t2.code = t.code and
                        t2.rturndte is null
                 );
SELECT t1.*
FROM table1 t1
WHERE NOT EXISTS(
    SELECT 'CODE'
    FROM table2 t2
    WHERE t1.code = t2.code
)
OR 
NOT EXISTS(
    SELECT 'NULLABLE'
    FROM table2 t2
    WHERE t2.code = t1.code
    AND t2.RTURNDTE IS NULL
)
AND t1.typeid = 'mn014'