简体   繁体   English

为什么此查询在MySQL 5.1.56中失败?

[英]why does this query fail in MySQL 5.1.56?

The following query fails in MySQL 5.1.56: 以下查询在MySQL 5.1.56中失败:

SELECT 
shop_id, products.product_id AS
product_id, brand, title, price, image, image_width, image_height

FROM products, users LEFT JOIN

(
    SELECT fav5.product_id AS product_id, SUM(CASE 
    WHEN fav5.current = 1 AND fav5.closeted = 1 THEN 1
    WHEN fav5.current = 1 AND fav5.closeted = 0 THEN -1
    ELSE 0
    END) AS favorites_count
    FROM favorites fav5
    GROUP BY fav5.product_id 

) AS fav6 ON products.product_id=fav6.product_id
WHERE products.product_id= 46876 AND users.user_id!=products.product_id

The error is 错误是

#1054 - Unknown column 'products.product_id' in 'on clause'

This modification without the user table does not fail: 没有用户表的修改不会失败:

SELECT 
shop_id, products.product_id AS
product_id, brand, title, price, image, image_width, image_height

FROM products LEFT JOIN

(
    SELECT fav5.product_id AS product_id, SUM(CASE 
    WHEN fav5.current = 1 AND fav5.closeted = 1 THEN 1
    WHEN fav5.current = 1 AND fav5.closeted = 0 THEN -1
    ELSE 0
    END) AS favorites_count
    FROM favorites fav5
    GROUP BY fav5.product_id 

) AS fav6 ON products.product_id=fav6.product_id
WHERE products.product_id= 46876

Neither query fails in MySQL 5.0.67. 在MySQL 5.0.67中,查询均不会失败。 (I exported the database from 5.0.67 and imported into 5.1.56 so the structure should be identical.) (我从5.0.67导出数据库,然后导入5.1.56,因此结构应相同。)

The products table does have a product_id column, of type int(10). 产品表确实有一个int(10)类型的product_id列。 The favorites table also has a product_id column of type int(10). 收藏夹表还具有类型为int(10)的product_id列。 What is going on? 到底是怎么回事?

As easy as swap from tables order: 与从表顺序交换一样容易:

FROM users, products LEFT JOIN

Be careful, you are mixing join notations . 请注意,您正在混合联接符号

JOIN processing operator precedence has changed in MySQL in 5.1. 在MySQL中,JOIN处理运算符的优先级在5.1中已更改。 It's a common problem for people upgrading from 5.0 MySQL LEFT JOIN after 5.0.12 changes - How to rewrite query 在5.0.12更改后 ,人们从5.0 MySQL LEFT JOIN升级是一个常见问题-如何重写查询

This is your original query, reformatted a little and with two parentheses added: 这是您的原始查询,对其进行了重新格式化,并添加了两个括号:

SELECT shop_id, products.product_id AS
       product_id, brand, title, price, image, image_width, image_height
  FROM products, 
       (  -- Parenthesis added
       users LEFT JOIN
       (
            SELECT fav5.product_id AS product_id, SUM(CASE 
            WHEN fav5.current = 1 AND fav5.closeted = 1 THEN 1
            WHEN fav5.current = 1 AND fav5.closeted = 0 THEN -1
            ELSE 0
            END) AS favorites_count
            FROM favorites fav5
            GROUP BY fav5.product_id 
        ) AS fav6 ON products.product_id=fav6.product_id
    )  -- Parenthesis added
WHERE products.product_id= 46876 AND users.user_id!=products.product_id

The parentheses indicate how the SQL parser is interpreting the query, and there is no products table within the added parenthesis. 括号表示SQL解析器如何解释查询,并且括号中没有products表。

It is a bad idea to mix the old style and new (as in, since SQL-92) style joins. 混合使用旧样式和新样式(例如,自SQL-92起)是一个坏主意。

Use: 采用:

SELECT shop_id, products.product_id AS
       product_id, brand, title, price, image, image_width, image_height
  FROM products JOIN users ON users.user_id != products.product_id
  LEFT JOIN
       (SELECT fav5.product_id AS product_id,
               SUM(CASE WHEN fav5.current = 1 AND fav5.closeted = 1 THEN 1
                        WHEN fav5.current = 1 AND fav5.closeted = 0 THEN -1
                        ELSE 0
                   END) AS favorites_count
          FROM favorites fav5
         GROUP BY fav5.product_id 
       ) AS fav6 ON products.product_id=fav6.product_id
 WHERE products.product_id = 46876

The != join is going to be slow (it practically a Cartesian product). !=连接将会很慢(实际上是笛卡尔积)。

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

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