简体   繁体   English

将两个SQL查询合并为一个

[英]Combining two SQL queries into one

I have two sql queries (which both work independently) that I am trying to combine: 我尝试合并两个SQL查询(它们都独立工作):

Query #1: 查询1:

select 
    N1."DateAndTime",
    N0."Ration" 
from 
    (("dbo"."AnimalFeedDailyConsumption" N0 
inner join 
    "dbo"."AnimalHistoricalData" N1 on (N0."OID" = N1."OID")) 
left join 
    "dbo"."BasicAnimal" N2 on (N1."BasicAnimal" = N2."OID"))
where 
    (N1."LactationNumber" = 4) and (N1."GCRecord" is null and (N2."Number" = 511))

Query #2: 查询2:

select 
   N2."Number",
   N1."DIM",
   N1."DateAndTime",
   N1."LactationNumber",
   N0."TotalYield"
from 
   (("dbo"."DailyMilk" N0 
inner join 
   "dbo"."AnimalHistoricalData" N1 on (N0."OID" = N1."OID")) 
left join 
   "dbo"."BasicAnimal" N2 on (N1."BasicAnimal" = N2."OID"))
where 
   (N1."LactationNumber" = 4) and (N1."GCRecord" is null and (N2."Number" = 511))

I tried this which resulted in nulls for 'Ration': 我尝试了这个,导致“ Ration”为空:

select 
    N2."Number",
    N1."DIM",
    N1."DateAndTime",
    N1."LactationNumber",
    N0."TotalYield",
    N3."Ration"
from 
    (("dbo"."DailyMilk" N0 
inner join 
    "dbo"."AnimalHistoricalData" N1 on (N0."OID" = N1."OID")) 
left join 
    ("dbo"."AnimalFeedDailyConsumption" N3 
inner join 
    "dbo"."AnimalHistoricalData" N1 on (N3."OID" = N1."OID"))
left join 
    "dbo"."BasicAnimal" N2 on (N1."BasicAnimal" = N2."OID"))
where 
    (N1."LactationNumber" = 4) and (N1."GCRecord" is null and (N2."Number" = 511))

So then I tried this which also resulted in nulls for 'Ration': 因此,我尝试了此操作,这也导致“ Ration”为空:

select 
   N2."Number",
   N1."DIM",
   N1."DateAndTime",
   N1."LactationNumber",
   N0."TotalYield",
   N3."Ration"
from
   (("dbo"."DailyMilk" N0 
inner join 
   "dbo"."AnimalHistoricalData" N1 on (N0."OID" = N1."OID")) 
left join 
   "dbo"."AnimalFeedDailyConsumption" N3 on (N3."OID" = N1."OID")
left join 
   "dbo"."BasicAnimal" N2 on (N1."BasicAnimal" = N2."OID"))
where 
   (N1."LactationNumber" = 4) and (N1."GCRecord" is null and (N2."Number" = 511))

This is hard to answer because I don't know the relationships between your tables. 这很难回答,因为我不知道您的表之间的关系。 In your "working" queries, you are testing for conditions on the RIGHT table (N2), which you joined with LEFT OUTER because it is optional - perhaps this is correct, but doesn't look right. 在“工作”查询中,您正在测试RIGHT表(N2)上的条件,该表与LEFT OUTER一起加入是因为它是可选的-也许这是正确的,但看起来并不正确。

How about this, with all joins converted to inner joins?: 将所有联接都转换为内部联接又如何呢?

select 
  N2."Number",
  N1."DIM",
  N1."DateAndTime",
  N1."LactationNumber",
  N0."TotalYield",
  N3."Ration"
from "dbo"."DailyMilk" N0 
inner join "dbo"."AnimalHistoricalData" N1       on N0."OID" = N1."OID" 
inner join "dbo"."AnimalFeedDailyConsumption" N3 on N3."OID" = N1."OID"
inner join "dbo"."BasicAnimal" N2                on N1."BasicAnimal" = N2."OID"
where N1."LactationNumber" = 4 
and   N1."GCRecord" is null 
and   N2."Number" = 511

Style Comment: BTW it is hard (for me) to understand which table is N0, N1, N3 & N3. 风格注释:顺便说一句,(对我而言)很难理解哪个表是N0,N1,N3和N3。 Why not alias them as BA, AHD, DM and AFDC? 为什么不将它们别名为BA,AHD,DM和AFDC? Also, too many (redundant) braces. 另外,括号过多(多余)。

Note: I can't test this out since I don't have your tables or data. 注意:由于我没有您的表格或数据,因此无法测试。 How about you create a SQLfiddle? 您如何创建SQLfiddle?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM