Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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_Postgresql - Fatal编程技术网

如何将SQL中的当前查询转换为不同的数据

如何将SQL中的当前查询转换为不同的数据,sql,postgresql,Sql,Postgresql,我正在尝试使用SQL进行此查询 select homework from karlos except select k_id from yara where y_ID is not null; 更新:基本上,我只想将最上面的查询重写为另一个输出相同结果的查询,但使用没有子查询或集合操作的外部联接我想您希望不存在: select s.ID from student where not exists (select 1 from advisor a

我正在尝试使用SQL进行此查询

select homework
from karlos
except
select k_id
from yara
where y_ID is not null;

更新:基本上,我只想将最上面的查询重写为另一个输出相同结果的查询,但使用没有子查询或集合操作的外部联接

我想您希望
不存在

select s.ID
from student
where not exists (select 1
                  from advisor a
                  where a.s_id = s.id and a.i_id is not null
                 );

从你的询问中我了解到,这应该是可行的

SELECT ID FROM student s JOIN advisor a ON s.ID IS NOT IN(a.s_id) WHERE a.i_id IS NOT NULL

您必须
left join
student
到一个查询,该查询返回
advisor
s\u id
,其中
i\u id不为null
,并在
where
子句中返回不匹配的行:

select s.ID
from student s left join (
  select s_id from advisor
  where i_ID is not null
) a on a.s_id = s.ID
where a.s_id is null
根据您的数据,这将在没有子查询的情况下工作:

select s.ID
from student s left join advisor a
on a.s_id = s.ID
where a.s_id is null or (a.s_id is not null and a.i_ID is null)

不管它是“不存在”还是其他什么,但关键是在不使用子查询或集合操作的情况下执行它,您只在查询中使用了“and”,并且使用了子查询。我试着不使用集合运算或子查询来做这件事你的查询有什么问题?是的,你100%正确,我只是试着修改你的查询,看看它是否适用于我的情况,你得到了我的想法你的查询有什么问题,但你的想法正是我想要的。非常感谢。@SQLnew为什么要编辑答案并删除对你有用的第二个查询?当我告诉eariler将其更改为原始查询时,我指的是第一个查询。