mysql版本为5.7.10时查询出错(在mysql 5.6.20中有效)

时间:2016-03-12 16:13:45

标签: mysql database

我需要迁移到另一台服务器主机,MySQL版本不同。我要求更改版本,但是这不会发生,我的网站会返回错误。

主要错误在于此查询:

SELECT p.*
FROM product p INNER JOIN
     account a
     ON a.id = p.account_id INNER JOIN
     supplier s
     ON a.id = s.account_id
 WHERE p.status = 1 AND a.type = 'supplier' AND
       s.id IN (SELECT supplier_id FROM supplier_location WHERE location_id IN (SELECT _id FROM (SELECT @r AS _id, (SELECT @r := parent_id FROM location WHERE id = _id) AS parent_id FROM (SELECT @r := 3523) vars, location h WHERE @r <> 0) T1 ) ) AND
       p.name LIKE '%nira%'
ORDER BY p.category_id, p.account_id, p.name ASC

在MySQL 5.6.20中可行,但在MySQL 5.7.10中返回此错误:

Unknown column '_id' in 'where clause'

有人知道如何解决?

感谢。

2 个答案:

答案 0 :(得分:0)

mysql中有一个错误https://bugs.mysql.com/bug.php?id=79549 你应该重写你的查询。问题在于the_ID是一个别名,并且那些在clausule中没有被删除。

答案 1 :(得分:0)

您可以通过更改

来修复它
(SELECT @r := parent_id FROM location WHERE id = _id)

(SELECT @r := parent_id FROM location WHERE id = @r)

修改后的查询:

SELECT p.*
FROM product p INNER JOIN
     account a
     ON a.id = p.account_id INNER JOIN
     supplier s
     ON a.id = s.account_id
 WHERE p.status = 1 AND a.type = 'supplier' AND
       s.id IN (SELECT supplier_id FROM supplier_location WHERE location_id IN (SELECT _id FROM (SELECT @r AS _id, (SELECT @r := parent_id FROM location WHERE id = @r) AS parent_id FROM (SELECT @r := 3523) vars, location h WHERE @r <> 0) T1 ) ) AND
       p.name LIKE '%nira%'
ORDER BY p.category_id, p.account_id, p.name ASC