简体   繁体   中英

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

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. 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.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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