简体   繁体   English

在where条件(Mysql)之后,在内部联接(在同一表上)之后执行与第二个表的左联接

[英]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 这是我的数据库: 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: 我已经成功地从bott_no_mgmt_data中提取了所有唯一的行,其中字段random_no是最高的,而bottle_no是唯一的,其代码如下:

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. 因为尽管它们的瓶号相同,但前者的random_no更高。

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. 我现在希望将每个返回的行“ bottleno”与另一个表“ sample”进行比较,该表仅包含字段“ bottleno”和瓶号列表。 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: 我假设我们将使用数据库“ sample”将上述结果“ LEFT JOIN”,如下所示:

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; 1064-您的SQL语法有误; 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 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第7行的“在LEFT JOIN sample ON sample.bottleno = yt.bottleno WHERE sample.bottleno IS NULL”附近使用

All joins should be written before Where clause as Daan mentions: 所有联接都应在Daan提到的Where子句之前编写:

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. -这意味着您想要的是INNER JOIN而不是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

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

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