Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.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 - Fatal编程技术网

Mysql 在中嵌套选择

Mysql 在中嵌套选择,mysql,Mysql,我正在使用Mysql并尝试将数据从一个数据库迁移到另一个数据库。我以前没有在数据库上工作过 这个查询给了我将近300个结果 select distinct internal_id from ratingsapp.hotel03 但是这个没有返回结果,也没有错误: select restname from City.resturant where restid not in (select distinct internal_id from ratingsapp.hotel03) 现

我正在使用Mysql并尝试将数据从一个数据库迁移到另一个数据库。我以前没有在数据库上工作过

这个查询给了我将近300个结果

select distinct internal_id from ratingsapp.hotel03
但是这个没有返回结果,也没有错误:

select restname from City.resturant where restid not in 
    (select distinct internal_id from ratingsapp.hotel03)

现在,如果我手动从hotel03表中选取几个内部_id,并将其放置在嵌套查询的位置,查询将返回正确的结果。我不确定我到底做错了什么

当其中一个值为
NULL
时,通常会发生这种情况。因此,这可能会起作用:

select restname
from City.resturant
where restid not in (select distinct internal_id from ratingsapp.hotel03 where internal_id is not null);
编写此查询的另一种方法是使用
不存在

select restname
from City.resturant r
where not exists (select 1
                  from ratingsapp.hotel03 h
                  where h.internal_id = r.restid
                 );

按照这种工作方式,
NULL
被正确处理,而无需直接检查它。这就是为什么
不存在
不存在更可取的原因之一
你确定这不是因为所有来自
城市的
restid
餐厅
都在
评级酒店03的
内部id
中吗?您说您手动选择了这些ID中的一些,结果是这样的,但请检查以下内容:

distinct City.restaurant.restid: 1, 2, 3, 4, 5
distinct ratingsapp.hotel03.internal_id: 1, 2, 3, 4, 5
然后您的查询将不返回任何内容,因为所有
restid
都不在
内部id
中。但是如果您从
评级sapp.hotel03.internal\u id
中选择一些id,例如:

select restname
from City.resturant
where restid not in (1, 2, 3)
然后,您将拥有所有的
City.restaurant
,其
restid
值为4或5