简体   繁体   English

Mysql右外部连接无法正常工作100%

[英]Mysql Right Outer Join is Not working 100 %

I am new to mysql and have an My sql query wrote and tryin to make an outer join with two subqueries but results are not showing accurate results here is the Query 我是mysql的新手,编写了My sql查询,并尝试用两个子查询进行外部联接,但是结果未在此处显示准确的结果

SELECT a.lpcode,a.lpname,a.companycode,a.zone,a.tdy_growr,a.tdy_acres,b.tdate_growr,tdate_acres,a.name
  FROM    (SELECT z.lpcode,
                  x.companycode,
                  z.lpname,
                  z.zone,
                  z.name,
                  count(x.vehicleno) tdy_growr,
                  sum(x.haulagecode) tdy_acres
             FROM gis.registration x, loadingpoint z
            WHERE x.date =
                     (SELECT max(a.date)
                        FROM gis.registration a
                       WHERE     a.fieldno > 0
                             AND a.haulagecode > 0
                             AND a.isaccepted = 1)
                  AND z.lpcode = x.lpcode
                  AND x.fieldno > 0
                  AND x.haulagecode > 0
                  AND x.isaccepted = 1
           GROUP BY x.lpcode) a
       RIGHT OUTER JOIN
          (SELECT r.lpcode,
                  count(r.vehicleno) tdate_growr,
                  sum(r.haulagecode) tdate_acres
             FROM gis.registration r, loadingpoint l
            WHERE r.fieldno > 0 AND r.haulagecode > 0 AND r.isaccepted = 1
            AND r.lpcode = l.lpcode
           GROUP BY l.lpcode) b
       ON a.lpcode = b.lpcode
ORDER BY a.zone, a.lpcode

Any Help May Appreciated thanks in advance 可能需要任何帮助,在此先感谢您

Right outer joins have terrible performance and there is not too many cases where you can't change the structure of the query to use a left outer join instead which will perform better. 右外部联接的性能很差,在很多情况下,您不能更改查询的结构以使用左外部联接来代替,这会带来更好的性能。 What your query is doing is getting all rows from b regardless of whether or not there is a row in a to join to. 您的查询正在执行的是从b获取所有行,而不管a中是否有要连接的行。 The same query rewritten is: 重写的相同查询是:

SELECT             a.lpcode,a.lpname,a.companycode,a.zone,a.tdy_growr,a.tdy_acres,b.tdate_growr,a.tdate_acres,a.name
FROM (SELECT r.lpcode,
              count(r.vehicleno) tdate_growr,
              sum(r.haulagecode) tdate_acres
         FROM gis.registration r, loadingpoint l
        WHERE r.fieldno > 0 AND r.haulagecode > 0 AND r.isaccepted = 1
        AND r.lpcode = l.lpcode
       GROUP BY l.lpcode) b
   LEFT OUTER JOIN (SELECT z.lpcode,
              x.companycode,
              z.lpname,
              z.zone,
              z.name,
              count(x.vehicleno) tdy_growr,
              sum(x.haulagecode) tdy_acres
         FROM gis.registration x, loadingpoint z
        WHERE x.date =
                 (SELECT max(a.date)
                    FROM gis.registration a
                   WHERE     a.fieldno > 0
                         AND a.haulagecode > 0
                         AND a.isaccepted = 1)
              AND z.lpcode = x.lpcode
              AND x.fieldno > 0
              AND x.haulagecode > 0
              AND x.isaccepted = 1
       GROUP BY x.lpcode) a
   ON a.lpcode = b.lpcode
ORDER BY a.zone, a.lpcode

This also gives all rows from b regardless of whether there is a matching row in a to join to. 这也将给出b中的所有行,而不管a中是否有匹配的行要连接。

If your problem is that you are getting NULLS in all of these columns: "a.lpcode,a.lpname,a.companycode,a.zone,a.tdy_growr,a.tdy_acres,a.tdate_acres,a.name" then use should be using the structure you are already using but with a LEFT OUTER JOIN instead which may return a null in this column "b.tdate_growr" 如果您的问题是在所有这些列中都得到NULL:“ a.lpcode,a.lpname,a.companycode,a.zone,a.tdy_growr,a.tdy_acres,a.tdate_acres,a.name”,请使用应该使用您已经在使用的结构,但使用LEFT OUTER JOIN代替,该结构可能在此列“ b.tdate_growr”中返回null

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

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