简体   繁体   中英

Perform a left join with a second table after an inner join (on same table) after where conditions (Mysql)

Here is my Database: bott_no_mgmt_data

random_no ; company_id ; bottle_no ; date       ; returned ; returned_to_stock ; username
    30201 ; MY COMP    ;         1 ; 2015-04-28 ;       10 ; NULL              ; ANDREW
    30202 ; MY COMP    ;         2 ; 2015-04-28 ;       10 ; NULL              ; ANDREW
    30205 ; MY COMP    ;         5 ; 2015-04-28 ;       10 ; NULL              ; ANDREW
    30208 ; MY COMP    ;         8 ; 2015-04-28 ;       10 ; NULL              ; ANDREW
    30209 ; MY COMP    ;         9 ; 2015-04-28 ;       10 ; NULL              ; ANDREW
    30210 ; MY COMP    ;        10 ; 2015-04-28 ;       10 ; NULL              ; ANDREW
    30211 ; MY COMP    ;         1 ; 2015-04-29 ;       20 ; NULL              ; ANDREW
    30212 ; MY COMP    ;         2 ; 2015-04-29 ;       20 ; NULL              ; ANDREW
    30213 ; MY COMP    ;         9 ; 2015-04-29 ;       30 ; NULL              ; ANDREW
    30214 ; MY COMP    ;        10 ; 2015-04-29 ;       30 ; NULL              ; ANDREW

I have successfully pulled all the entire unique rows from bott_no_mgmt_data where the field random_no is highest and bottle_no is unique with the following code:

select yt.* 
  from bott_no_mgmt_data yt<br>
       inner join(select bottle_no, max(random_no) random_no
                    from bott_no_mgmt_data 
                   WHERE username = 'ANDREW' 
                   group by bottle_no) ss on yt.bottle_no = ss.bottle_no 
                                         and yt.random_no = ss.random_no  
where returned < 15 and date > '2015-04-01'

So for example one of the rows it returns will be

30214;MY COMP;10;2015-04-29;30;NULL;ANDREW 

and NOT

30210;MY COMP;10;2015-04-28;10;NULL;ANDREW

because while their bottleno's are the same the former's random_no is higher.

My Problem:

I now wish to compare each returned rows 'bottleno' with another table 'sample' which simply contains field 'bottleno' with a list of bottle numbers. I wish to compare them and only return those that match. I assume we would then 'LEFT JOIN' the results above with database 'sample' as below:

select yt.* from bott_no_mgmt_data yt<br>
       inner join(select bottle_no, max(random_no) random_no
                    from bott_no_mgmt_data WHERE username = 'ANDREW' 
                   group by bottle_no) ss on yt.bottle_no = ss.bottle_no and yt.random_no = ss.random_no 
 where returned < 15 and date > '2015-04-01'
       LEFT JOIN sample ON sample.bottleno = yt.bottle_no

The extra left join gives me an error

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN sample ON sample.bottleno = yt.bottleno WHERE sample.bottleno IS NULL ' at line 7

All joins should be written before Where clause as Daan mentions:

select yt.* from bott_no_mgmt_data yt
inner join(
    select bottle_no, max(random_no) random_no 
    from bott_no_mgmt_data 
    WHERE username = 'ANDREW' group by bottle_no
)
ss on yt.bottle_no = ss.bottle_no and yt.random_no = ss.random_no 
LEFT JOIN sample ON sample.bottleno = yt.bottle_no
where returned < 15 and date > '2015-04-01'

A couple of things. You don't need that first inner join at all, it's pointless. Also, you said "I wish to compare them and only return those that match." - so that means you want INNER JOIN not LEFT JOIN.

SELECT MAX(random_no) AS random_no, company_id, yt.bottle_no, `date`, returned, username
FROM bott_no_mgmt_data yt
INNER JOIN sample ON sample.bottle_no=yt.bottle_no
WHERE yt.username = 'ANDREW' 
  AND yt.returned < 15 
  AND yt.date > '2015-04-01'
GROUP BY company_id, yt.bottle_no, `date`, returned, username

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