Sql 如何在同一表上有查询的情况下选择查询
我试过这个Sql 如何在同一表上有查询的情况下选择查询,sql,postgresql,Sql,Postgresql,我试过这个 select case when exists (select * from rnr.company_faqs fo where fo.mp_company_uid=734) then select * from rnr.company_faqs fo where fo.mp_company_uid=734 else select * from rnr.faq_master end 我犯了一个错误 ERROR: syntax error at
select case when exists (select * from rnr.company_faqs fo where fo.mp_company_uid=734)
then select * from rnr.company_faqs fo where fo.mp_company_uid=734
else select * from rnr.faq_master
end
我犯了一个错误
ERROR: syntax error at or near "select"
LINE 2: then select * from rnr.company_faqs fo where fo.mp...
^
******错误******
只需用左括号和右括号括起您的选择:
当您尝试手动编写时,您将得到如下脚本:
select select * from rnr.company_faqs fo where fo.mp_company_uid = 734
这会给你一个错误
但如果你把它包装成:
如果表rnr.company\u faq和rnr.faq\u master中的结果集列相同,则脚本将正确。 您应该尝试选择数据
Select * from (
select * from rnr.company_faqs fo where fo.mp_company_uid=734
UNION
select * from rnr.faq_master
) a;
如果rnr.company\u常见问题解答有结果集,它将给出一行,否则返回null
并集结果集将是交集
您可以修改查询以获得所需的结果集
希望有帮助 这里的问题是,当只使用标量数据而不使用SELECT*返回行集时,可以使用CASE。请使用UNION尝试以下操作:
select * from rnr.company_faqs fo where fo.mp_company_uid=734
UNION ALL
select * from rnr.faq_master
WHERE NOT EXISTS(select *
from rnr.company_faqs fo
where fo.mp_company_uid=734)
@Zafta你有试着运行它吗?它显示出任何错误吗?
select (select * from rnr.company_faqs fo where fo.mp_company_uid = 734)
Select * from (
select * from rnr.company_faqs fo where fo.mp_company_uid=734
UNION
select * from rnr.faq_master
) a;
select * from rnr.company_faqs fo where fo.mp_company_uid=734
UNION ALL
select * from rnr.faq_master
WHERE NOT EXISTS(select *
from rnr.company_faqs fo
where fo.mp_company_uid=734)