Firebird到MySQL查询迁移 – 选择内部连接子查询
作者:互联网
我有一个在我们的Firebird SQL数据模块中工作的查询.
我们迁移到MySQL,除了这个,我的所有查询都没有问题.
请帮我解决这个问题.我收到一个错误:
Failed to Execute. Unknown column ‘part.id’ in ‘on clause’
我的Firebird查询:
SELECT vendor.name AS "Vendor Name",
Cast(Cast(vendorparts.lastdate AS date) AS CHAR(10)) AS "Last Date",
CASE product.price
WHEN '0' THEN 'CONFIRM'
WHEN NULL THEN 'CONFIRM'
ELSE Round(product.price, 2)
end AS "D-Price",
Cast(vendorparts.lastcost AS DECIMAL(18, 2)) AS "Last Cost",
Cast(lowestcost.lowestcost AS DECIMAL(18, 2)) AS "Lowest Cost",
Cast(highestcost.highestcost AS DECIMAL(18, 2)) AS "Highest Cost",
part.num AS "Part Number",
part.description AS "Part Description"
FROM vendor,
vendorparts,
part,
product
INNER JOIN (SELECT vendorparts.partid,
Max(vendorparts.lastcost) AS Highestcost
FROM vendorparts
GROUP BY vendorparts.partid) AS highestcost
ON part.id = highestcost.partid
INNER JOIN (SELECT vendorparts.partid,
Min(vendorparts.lastcost) AS Lowestcost
FROM vendorparts
GROUP BY vendorparts.partid) AS lowestcost
ON part.id = lowestcost.partid
WHERE vendor.id = vendorparts.vendorid
AND product.partid = part.id
AND vendorparts.partid = part.id
AND vendorparts.lastcost <> 0
除了在子查询中创建的最低成本和最高成本之外,所有表都在数据库中.
希望我的要求写得清楚.但总而言之 – 我需要将这个工作的Firebird查询迁移到MySQL中.
为什么这会在Firebird中运行而不是在MySQL中运行?
解决方法:
问题是在mySQL中comma operator has lower precedence比连接运算符,因此part.id = highestcost.partid上的产品内连接(子查询)part.id = lowestcost.partid连接在part表之前进行评估加入表达式,因此出现错误信息.
用简单的连接运算符替换逗号运算符,并将连接条件从where子句移到on子句,并且一切正常:
...
FROM vendor
inner join vendorparts on vendor.id = vendorparts.vendorid
inner join part on vendorparts.partid = part.id
inner join product on product.partid = part.id
INNER JOIN (SELECT vendorparts.partid,
Max(vendorparts.lastcost) AS Highestcost
FROM vendorparts
GROUP BY vendorparts.partid) AS highestcost
ON part.id = highestcost.partid
INNER JOIN (SELECT vendorparts.partid,
Min(vendorparts.lastcost) AS Lowestcost
FROM vendorparts
GROUP BY vendorparts.partid) AS lowestcost
ON part.id = lowestcost.partid
WHERE vendorparts.lastcost <> 0
如果你有更多这样的查询混合逗号运算符和显式联接,那么你应该检查它们,因为它们可能会产生不同的结果,即使MySQL中没有语法错误.
标签:mysql,join,select,nested,firebird 来源: https://codeday.me/bug/20190823/1699941.html