Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.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
SQL查询如果为空,则不返回行_Sql_Oracle - Fatal编程技术网

SQL查询如果为空,则不返回行

SQL查询如果为空,则不返回行,sql,oracle,Sql,Oracle,我是SQL新手,我想了解一下我的问题 我使用以下查询 select id, pid from assoc where id in (100422, 100414, 100421, 100419, 100423) 所有这些id都不需要pid,有些没有,有些有pid。目前它跳过没有pid的记录。 我想要一种方式,它将显示结果如下 pid id ----------- 703 100422

我是SQL新手,我想了解一下我的问题 我使用以下查询

  select id,
         pid 
    from assoc
   where id in (100422, 100414, 100421, 100419, 100423)
所有这些id都不需要pid,有些没有,有些有pid。目前它跳过没有pid的记录。 我想要一种方式,它将显示结果如下

         pid    id
         -----------
         703    100422
         313    100414
         465    100421
         null   100419
         null   100423

任何帮助都将不胜感激。谢谢

哦,我想我已经想到了:你必须枚举所有的
id
s和相应的
pid
s。如果没有相应的
pid
,则将
null
(一种外部联接)。如果是您的情况,则Oracle解决方案可以是:

   with 
     -- dummy: required ids
     dummy as (
                 select 100422 as id from dual
       union all select 100414 as id from dual
       union all select 100421 as id from dual
       union all select 100419 as id from dual
       union all select 100423 as id from dual),
     -- main: actual data we have
     main as (
       select id,
              pid 
         from assoc
           -- you may put "id in (select d.id from dummy d)"
        where id in (100422, 100414, 100421, 100419, 100423))

   -- we want to print out either existing main.pid or null
   select main.pid as pid,
          dummy.id as id
     from dummy left join main on dummy.id = main.id

哦,我想我已经想到了:你必须枚举所有的
id
s和相应的
pid
s。如果没有相应的
pid
,则将
null
(一种外部联接)。如果是您的情况,则Oracle解决方案可以是:

   with 
     -- dummy: required ids
     dummy as (
                 select 100422 as id from dual
       union all select 100414 as id from dual
       union all select 100421 as id from dual
       union all select 100419 as id from dual
       union all select 100423 as id from dual),
     -- main: actual data we have
     main as (
       select id,
              pid 
         from assoc
           -- you may put "id in (select d.id from dummy d)"
        where id in (100422, 100414, 100421, 100419, 100423))

   -- we want to print out either existing main.pid or null
   select main.pid as pid,
          dummy.id as id
     from dummy left join main on dummy.id = main.id
id
从其他表中获取,并且
assoc
仅具有与
id
关联的
pid

assoc
表似乎是用于在关系数据库中的两个实体之间实现连接的表

它只包含一个表中与另一个表中的实体相关的实体的条目。它不包含有关不在关系中的实体的信息,并且您希望获得的某些结果来自不在关系中的实体

您的问题的解决方案是
右键联接
id
来自的表,并将
where
条件与从原始表中检索到的值相对应(因为它包含您需要的行)。
RIGHT JOIN
确保右侧表中的所有匹配行都包含在结果集中,即使它们在左侧表中没有匹配行

假设
id
列来自的表名为
table1
,则需要的查询是:

SELECT assoc.id, assoc.pid 
FROM assoc
    RIGHT JOIN table1 ON assoc.id = table1.id
WHERE table1.id IN (100422, 100414, 100421, 100419, 100423)
id
从其他表中获取,并且
assoc
仅具有与
id
关联的
pid

assoc
表似乎是用于在关系数据库中的两个实体之间实现连接的表

它只包含一个表中与另一个表中的实体相关的实体的条目。它不包含有关不在关系中的实体的信息,并且您希望获得的某些结果来自不在关系中的实体

您的问题的解决方案是
右键联接
id
来自的表,并将
where
条件与从原始表中检索到的值相对应(因为它包含您需要的行)。
RIGHT JOIN
确保右侧表中的所有匹配行都包含在结果集中,即使它们在左侧表中没有匹配行

假设
id
列来自的表名为
table1
,则需要的查询是:

SELECT assoc.id, assoc.pid 
FROM assoc
    RIGHT JOIN table1 ON assoc.id = table1.id
WHERE table1.id IN (100422, 100414, 100421, 100419, 100423)

您的查询看起来正常,不能跳过任何
pid
(字段上没有筛选器)。尝试从pid为null的assoc中选择id、pid,或从pid为null且id=100423的assoc中选择id、pid,检查pid为null的记录。你有没有,比如说,
null 100423
recordHi,谢谢你的回复。问题是,当id没有pid时,根本没有条目。当我执行查询时,从pid为null的assoc中选择id,pid,它返回一个空行,表示没有pid时没有记录。您是否有
null
pid
是否从pid为null的assoc中选择id,pid
是否返回除空光标以外的任何内容?没有空值,仅返回空光标。如果pid没有“任何条目”,则表中不得存在关联的id?在这种情况下,您需要使用
union-all
添加另一个片段,它返回那些不存在的片段。您是否有另一个不同的“主id”表,其中包含所有id,包括没有pid的id?您的查询看起来正常,它不能跳过任何
pid
(字段上没有筛选器)。尝试从pid为null的assoc中选择id、pid,或从pid为null且id=100423的assoc中选择id、pid,检查pid为null的记录。你有没有,比如说,
null 100423
recordHi,谢谢你的回复。问题是,当id没有pid时,根本没有条目。当我执行查询时,从pid为null的assoc中选择id,pid,它返回一个空行,表示没有pid时没有记录。您是否有
null
pid
是否从pid为null的assoc中选择id,pid
是否返回除空光标以外的任何内容?没有空值,仅返回空光标。如果pid没有“任何条目”,则表中不得存在关联的id?在这种情况下,您需要使用
union-all
添加另一个片段,它返回那些不存在的片段。您是否有另一个不同的“主id”表,其中包含所有id,包括没有pid的id?谢谢您,Dmitry,我模糊地理解这一点,但是这些数据块是如何相互关联的?最后一个选择是独立的?
dummy
返回所需的ID,
main
-实际数据
left join
将它们组合在一起。很好,我无法在sql developer中运行查询。如何将最后一部分“select main.pid as pid,dummy.id as id from dummy left join main on dummy.id=main.id”与其他两部分连接起来?请提供帮助。
with
see允许我将查询分解为片段
dummy
main
,并将它们组合为