Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/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-如何从父表中获取缺少的所有关系外键?_Mysql_Sql - Fatal编程技术网

mysql-如何从父表中获取缺少的所有关系外键?

mysql-如何从父表中获取缺少的所有关系外键?,mysql,sql,Mysql,Sql,我为旧项目工作,不包括关系外键。缺少某些类别。我想获取类别\u id在类别\u tbl中不存在的所有产品 category_tbl id name 1 fruit 2 animal product_tbl id name category_id 1 apple 1 2 cat 2 3 coffee 3 4 tea 3 下表中有类别\u id 3不存在于类别\u tbl中

我为旧项目工作,不包括关系外键。缺少某些类别。我想获取类别\u id在类别\u tbl中不存在的所有产品

   category_tbl
    id name
     1 fruit
     2 animal

  product_tbl
     id name   category_id
     1  apple   1
     2  cat     2
     3  coffee  3
     4  tea     3
下表中有类别\u id 3不存在于类别\u tbl中

   category_tbl
    id name
     1 fruit
     2 animal

  product_tbl
     id name   category_id
     1  apple   1
     2  cat     2
     3  coffee  3
     4  tea     3

您可以使用left join和chek来表示category_id为null的位置

 select  c.name
 from product_tbl p 
 left  join category_tbl c  ON p.category_id = c.id 
 where category_id  is null 

您可以使用left join和chek来表示category_id为null的位置

 select  c.name
 from product_tbl p 
 left  join category_tbl c  ON p.category_id = c.id 
 where category_id  is null 

您可以在不存在的情况下执行此操作:

select p.* 
from product_tbl p
where not exists (
  select 1 from category_tbl
  where id = p.category_id 
)
看。 结果:


您可以在不存在的情况下执行此操作:

select p.* 
from product_tbl p
where not exists (
  select 1 from category_tbl
  where id = p.category_id 
)
看。 结果:


我怎么才能得到相反的结果呢?类别id不在producs表格@forpas thanksWith中,从类别中选择id,其中id不在从产品中选择的类别\u id中\u tbl如何获得相反的结果?类别id不在producs表格@forpas thanksWith中,从类别\u tbl中选择id,其中id不在从产品\u tbl中选择类别\u id中