【问题标题】:SELECT WHERE filter is not applied after using a LEFT JOIN使用 LEFT JOIN 后不应用 SELECT WHERE 过滤器
【发布时间】:2022-01-05 13:45:11
【问题描述】:

假设我有三个表 transactionsitemsusers,看起来像这样

#items

id  brand  price   transaction_id
--+------+-------+---------------
1  apple    10          1
2  apple    20          2
3  pear     15          1
4  banana   20          1
#transactions
id   user_id
--+------+
1    1    
2    1  
3    2
4    1
#users

id   system   value
---+---------+-------
1    gooogle   001
1    facebook  jashd28
2    google    002
2    facebook  jlak30

然后我想将它们连接在一起,这样我就可以看到哪个(谷歌)用户购买了哪些品牌。目前我的查询看起来像

SELECT items.transaction_id,items.brand,users.value, items.price  FROM items
LEFT JOIN transactions 
    ON transactions.id= items.transactions_id
LEFT JOIN users
    ON users.id=transactions.user_id
    AND users.system='google'
WHERE items.transaction_id=1
AND LOWER(items.brand) LIKE '%apple%' OR LOWER(items.brand) LIKE '%pear%'

(上面的查询和我的不是100%一样,但是结构是一样的;多个join,最后一个WHERE子句)

但它返回 transaction_id 而不是 1。我可以将 items.transaction_id 放入 JOIN 中,但它似乎从 transaction_id=1 返回所有项目,而不是 applepear

预期:

id brand price user
--+-----+-----+-----
1  apple  10    001
3  pear   15    001

结果:

id brand price user
--+-----+-----+-----
1  apple  10    001
2  apple  20    002  #<- should not be there since it does not have transaction_id=1
3  pear   15    001
4  banana 20    001    

【问题讨论】:

  • 使用括号:AND (LOWER(items.brand) LIKE '%apple%' OR LOWER(items.brand) LIKE '%pear%')
  • @forpas pfft, perentheses; “括号”的时髦术语啊哈哈哈哈 :-) 编辑我的答案听起来更聪明

标签: mysql left-join


【解决方案1】:

您的陈述如下:

SELECT items.transaction_id,items.brand,users.value, items.price  FROM items
LEFT JOIN transactions 
    ON transactions.id= items.transactions_id
LEFT JOIN users
    ON users.id=transactions.user_id
    AND users.value='google'
WHERE items.transaction_id=1
AND LOWER(items.brand) LIKE '%apple%' OR LOWER(items.brand) LIKE '%pear%'

您的问题是它显示的值没有items.transaction_id = 1

原因是因为您混淆了 ANDOR SQL 限定符,并且因为您没有正确地将它们括起来(用括号)它们的顺序很重要,所以你正在做的有效:

WHERE (
       items.transaction_id=1
    AND 
       LOWER(items.brand) LIKE '%apple%'
     )
 OR LOWER(items.brand) LIKE '%pear%'

您的AND 语句应使用括号,因为您有一个OR 限定符;所以需要构造AND ( x OR z )的语法逻辑

所以:

WHERE items.transaction_id=1
    AND ( LOWER(items.brand) LIKE '%apple%' 
          OR LOWER(items.brand) LIKE '%pear%')

【讨论】:

    【解决方案2】:

    正如 Martin 在上述问题中提到的,一个问题是缺少括号。 其次,您需要除了 JOIN 之外的 where 条件。 所以最终的查询是

    select distinct i.id, i.brand, i.price, u.value from items i
    left join transactions t
    on i.transaction_id = t.Id
    left join users u 
    on u.ID2 = t.user_id 
    where i.transaction_id = 1 
    and (i.brand like '%apple%' or i.brand like '%pear%')
    and u.system = 'google'
    

    【讨论】:

    • 这可能会完全改变查询的结果集,我看不出 OP 需要这种改变。
    • use text, not images/links, for text--including tables & ERDs。转述或引用其他文本。只提供您需要的东西并将其与您的问题联系起来。仅将图像用于无法表达为文本或增强文本的内容。在图片中包含图例/键和说明。
    猜你喜欢
    • 2016-07-21
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多