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