【问题标题】:Outer Join in mYsql QyerymYsql 查询中的外连接
【发布时间】:2012-04-14 18:28:24
【问题描述】:

我已经关注 MYsql 查询并尝试右外连接,但无法理解如何执行此操作

这里是查询请任何一个帮助

select lp_des.lpname,today.cnt_veh_tdy,todate.cnt_veh_tdate
from
(select distinct registration.lpcode,loadingpoint.lpname 
from registration,loadingpoint
where registration.lpcode=loadingpoint.lpcode) lp_des,
(select lpcode,count(vehicleno) cnt_veh_tdate
from registration
where registration.companycode='01'
group by lpcode) todate,
(
select lpcode,count(vehicleno) cnt_veh_tdy
from registration
where registration.companycode='01'
and registration.date=(select max(date) from registration)
group by lpcode) today
right outer join today on lp_des.lpcode = today.lpcode
right outer join todate on lp_des.lpcode = todate.lpcode

我想在这部分做右外连接

where lp_des.lpcode=todate.lpcode
and   lp_des.lpcode=today.lpcode

请帮助并提前致谢

【问题讨论】:

  • 必须是right outer join吗?也许你应该描述你想要的结果,而不是只发布你认为你需要的解决方案。
  • 另外,全世界都在使用left join (=left outer join)。 A 和 B 上的每个右连接都可以重写为左连接 B 和 A,这使其更具可读性。尤其是在一个查询中混合使用右连接和左连接会增加阅读、测试和修改的难度。
  • 我已经在上面的查询中进行了右外连接,现在请检查查询中可能出现的语法错误
  • 在这里发布整段代码真的比在right join上查看手册更容易吗?
  • 我也在使用查询和子查询,所以我显示了我的整个查询,所以很容易理解我仍然没有得到我的答案请将我的查询更改为实际的外部联接

标签: mysql outer-join


【解决方案1】:

你要求这个:

select 
  lp_des.lpname,
  today.cnt_veh_tdy,
  todate.cnt_veh_tdate
from
  (select distinct 
    r.lpcode,
    l.lpname 
  from 
    registration r
    inner join loadingpoint l on l.lpcode = r.lpcode) lp_des
  right join
    (select 
      r.lpcode,
      count(r.vehicleno) cnt_veh_tdate
    from 
      registration r
    where 
      r.companycode='01'
    group by 
      lpcode) todate on todate.lpcode = lp_des.lpcode
  right join
    (select 
      r.lpcode,
      count(r.vehicleno) cnt_veh_tdy
    from 
      registration r
    where 
      r.companycode = '01'
      and registration.date = (select max(date) from registration)
    group by 
      r.lpcode) today on today.lpcode = lp_des.lpcode

但我认为你的意思是:

select
  r.lpcode,
  l.lpname,
  count(r.vehicleno) cnt_veh_tdate,
  count(case when r.date = md.date then r.vehicleno else null end) cnt_veh_tdy
from
  registration r
  inner join (select max(rm.date) maxdate from registration rm) md
  left join loadingpoint l on l.lpcode = r.lpcode
where
  r.companycode = '01'
group by 
  r.lpcode

甚至可能是这样:

select
  r.lpcode,
  l.lpname,
  count(r.vehicleno) cnt_veh_tdate,
  count(case when r.date = date() then r.vehicleno else null end) cnt_veh_tdy
from
  registration r
  left join loadingpoint l on l.lpcode = r.lpcode
where
  r.companycode = '01'
group by 
  r.lpcode

如果我没看错的话,您需要一个查询来返回分配给装载点的公司 1 的车辆数量,以及仅今天的车辆数量。您还希望对尚未分配装载点的车辆进行计数。 虽然如果你添加了这个描述会有所帮助。它会帮助那些回答你问题的人,但它也会帮助你首先编写正确的查询。

【讨论】:

    【解决方案2】:

    右外连接的语法是:

    SELECT t1.id, t2.id FROM t1 RIGHT OUTER JOIN t2 ON t1.field1 = t2.field2
    

    如果您加入同一领域,您可以使用USING 而不是ON:

    SELECT t1.id, t2.id FROM t1 RIGHT OUTER JOIN t2 USING (field)
    

    【讨论】:

    • 请检查我的查询并在该查询中进行外部联接,您的示例不起作用
    猜你喜欢
    • 2013-02-25
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 2012-06-14
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多