使用“OR”组合两个单独的 SQL 查询的结果;

发布于 2024-10-17 05:42:19 字数 1489 浏览 3 评论 0原文

框架:Rails 2.3.8 数据库; PostgreSQL

我目前有以下 SQL 语句(由 Rails 生成)

SELECT "outbreaks".* FROM "outbreaks" INNER JOIN "bacterial_agents" ON bacterial_agents.outbreak_id = outbreaks.id INNER JOIN "bacteria" ON "bacteria".id = "bacterial_agents".bacterium_id WHERE (bacteria.name ILIKE E'%VTEC O157%')

SELECT "outbreaks".* FROM "outbreaks" INNER JOIN "viral_agents" ON viral_agents.outbreak_id = outbreaks.id INNER JOIN "virus" ON "virus".id = "viral_agents".virus_id WHERE (virus.name ILIKE E'%NOROVIRUS%')

和下表(我跳过了未使用的属性,布局/键命名遵循 Rails 约定)

outbreaks
  id

bacterial_agents
  id
  outbreak_id
  bacterium_id

bacteria
  id
  name

viral_agents
  id
  outbreak_id
  virus_id

viruses
  id
  name

任何想法我将如何加入上面两条带有“OR”条件的SQL语句,选择与细菌表相关的爆发记录,其中细菌名称为“VTEC O157”,或者与病毒表相关,其中病毒名称为“NOROVIRUS”?

*编辑

对 SQL 的 Rails 生成进行澄清 - 它当前输出以下 SQL

SELECT "outbreaks".* FROM "outbreaks" INNER JOIN "bacterial_agents" ON bacterial_agents.outbreak_id = outbreaks.id INNER JOIN "bacteria" ON "bacteria".id = "bacterial_agents".bacterium_id INNER JOIN "viral_agents" ON outbreaks.id = viral_agents.outbreak_id INNER JOIN "viruses" ON "viral_agents".virus_id = "viruses".id WHERE ((bacteria.name ILIKE E'%VTEC O157%') AND (viruses.name ILIKE E'%NOROVIRUS%')) LIMIT 1

本质上我想将前两个语句的组合结果输出为与上述语句类似的格式(用“OR”条件代替“与”)。但是,如果这是不可能的,我只需要编写一个范围来处理 SQL 联合。

从下面的回复来看,我将结合这两个单独的声明,欢呼:)

Framework: Rails 2.3.8
Database; PostgreSQL

I currently have the following SQL statements (generated by Rails)

SELECT "outbreaks".* FROM "outbreaks" INNER JOIN "bacterial_agents" ON bacterial_agents.outbreak_id = outbreaks.id INNER JOIN "bacteria" ON "bacteria".id = "bacterial_agents".bacterium_id WHERE (bacteria.name ILIKE E'%VTEC O157%')

SELECT "outbreaks".* FROM "outbreaks" INNER JOIN "viral_agents" ON viral_agents.outbreak_id = outbreaks.id INNER JOIN "virus" ON "virus".id = "viral_agents".virus_id WHERE (virus.name ILIKE E'%NOROVIRUS%')

With the following tables (I've skipped the non-used attributes and the layout / key naming is down to Rails conventions)

outbreaks
  id

bacterial_agents
  id
  outbreak_id
  bacterium_id

bacteria
  id
  name

viral_agents
  id
  outbreak_id
  virus_id

viruses
  id
  name

Any ideas how I would join the two above SQL statements with an "OR" condition, to select outbreak records which are related to the bacteria table where the bacteria.name is like "VTEC O157" OR related to the viruses table where the virus.name is like "NOROVIRUS"?

*EDIT

Abit of clarification on the Rails generation of the SQL - it currently outputs the following SQL

SELECT "outbreaks".* FROM "outbreaks" INNER JOIN "bacterial_agents" ON bacterial_agents.outbreak_id = outbreaks.id INNER JOIN "bacteria" ON "bacteria".id = "bacterial_agents".bacterium_id INNER JOIN "viral_agents" ON outbreaks.id = viral_agents.outbreak_id INNER JOIN "viruses" ON "viral_agents".virus_id = "viruses".id WHERE ((bacteria.name ILIKE E'%VTEC O157%') AND (viruses.name ILIKE E'%NOROVIRUS%')) LIMIT 1

Essentially I want to output the combined results from the first two statements into a similar format as the above statement (with an "OR" condition in place of the "AND"). However if this isnt possible I'll just have to write a scope to handle the SQL union instead.

Think judging by the replies below I'll go with a union of the two seperate statements, cheers :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾其所爱 2024-10-24 05:42:19

我认为您将 OR 与 SQL 联合运算符 混淆了,以连接两个查询的返回结果。

OR 用于管理语句 WHERE 部分中的条件,而 union 用于将一个查询附加到另一个查询。

您应该注意,只有当两个查询具有相同的域时,联合才会起作用。快速查看您的查询,它应该完全符合您的想法。

试试这个:

SELECT "outbreaks".* FROM "outbreaks" INNER JOIN "bacterial_agents" ON bacterial_agents.outbreak_id = outbreaks.id INNER JOIN "bacteria" ON "bacteria".id = "bacterial_agents".bacterium_id WHERE (bacteria.name ILIKE E'%VTEC O157%')
UNION
SELECT "outbreaks".* FROM "outbreaks" INNER JOIN "viral_agents" ON viral_agents.outbreak_id = outbreaks.id INNER JOIN "virus" ON "virus".id = "viral_agents".virus_id WHERE (virus.name ILIKE E'%NOROVIRUS%')

I think you are confusing OR with SQL union operator to join the returns of the two queries.

OR is used to manage conditions in the WHERE part of the statement while union is used to append one query to another.

You should note that union will only work if the two queries have the same domains. From quick look at your queries it should do exactly what you have in mind.

Try this:

SELECT "outbreaks".* FROM "outbreaks" INNER JOIN "bacterial_agents" ON bacterial_agents.outbreak_id = outbreaks.id INNER JOIN "bacteria" ON "bacteria".id = "bacterial_agents".bacterium_id WHERE (bacteria.name ILIKE E'%VTEC O157%')
UNION
SELECT "outbreaks".* FROM "outbreaks" INNER JOIN "viral_agents" ON viral_agents.outbreak_id = outbreaks.id INNER JOIN "virus" ON "virus".id = "viral_agents".virus_id WHERE (virus.name ILIKE E'%NOROVIRUS%')
一生独一 2024-10-24 05:42:19

只需在查询之间放置 UNION ALL

SELECT "outbreaks".* 
  FROM "outbreaks"
  INNER JOIN "bacterial_agents"     ON bacterial_agents.outbreak_id = outbreaks.id 
  INNER JOIN "bacteria"         ON "bacteria".id = "bacterial_agents".bacterium_id
WHERE (bacteria.name ILIKE E'%VTEC O157%')

UNION ALL

SELECT "outbreaks".*
   FROM "outbreaks"
   INNER JOIN "viral_agents"    ON viral_agents.outbreak_id = outbreaks.id 
   INNER JOIN "virus"       ON "virus".id = "viral_agents".virus_id
 WHERE (virus.name ILIKE E'%NOROVIRUS%')

Just place a UNION ALL between the queries

SELECT "outbreaks".* 
  FROM "outbreaks"
  INNER JOIN "bacterial_agents"     ON bacterial_agents.outbreak_id = outbreaks.id 
  INNER JOIN "bacteria"         ON "bacteria".id = "bacterial_agents".bacterium_id
WHERE (bacteria.name ILIKE E'%VTEC O157%')

UNION ALL

SELECT "outbreaks".*
   FROM "outbreaks"
   INNER JOIN "viral_agents"    ON viral_agents.outbreak_id = outbreaks.id 
   INNER JOIN "virus"       ON "virus".id = "viral_agents".virus_id
 WHERE (virus.name ILIKE E'%NOROVIRUS%')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文