SQL 使用 INNER JOIN 函数插入值时出错
SQL error using INNER JOIN function to insert values
我的简单 SQL 代码有问题,因为我只是一个初学者。
我正在尝试检索不同国家/地区的出站成本伙伴的平均值,并将其写入仓库的每个相应行。
我的代码如下:
INSERT INTO Warehouse (`inbound-cost-pal`)
SELECT cost.`average`
FROM Warehouse AS wh
INNER JOIN (
SELECT
AVG (`warehouse-cost-table`.`outbound-cost-pal`) AS `average`
FROM `warehouse-cost-table`
)cost
ON wh.`location-tariff-code` = LEFT(cost.`country`,2)
;
我收到错误消息“'on clause' 中的未知列 'cost.country'”。
您在子查询成本select中忘记了国家
INSERT INTO Warehouse (inbound-cost-pal)
SELECT cost.average
FROM Warehouse AS wh
INNER JOIN (
SELECT
AVG (warehouse-cost-table.outbound-cost-pal) AS average,
country
FROM warehouse-cost-table
)cost
ON wh.location-tariff-code = LEFT(cost.country,2)
;
如果您需要每个国家/地区的 avg(),那么您还需要子查询中的国家/地区
INSERT INTO Warehouse (`inbound-cost-pal`)
SELECT cost.`average`
FROM Warehouse AS wh
INNER JOIN (
SELECT `warehouse-cost-table`.`country`,
AVG(`warehouse-cost-table`.`outbound-cost-pal`) AS `average`
FROM `warehouse-cost-table`
GROUP BY `warehouse-cost-table`.`country`
) cost
ON wh.`location-tariff-code` = LEFT(cost.`country`,2)
;
否则,如果您不需要国家/地区的 avg() 而需要整体平均值,那么您可以使用交叉连接而不是内部连接
INSERT INTO Warehouse (`inbound-cost-pal`)
SELECT cost.`average`
FROM Warehouse AS wh
CROSS JOIN (
SELECT
AVG(`warehouse-cost-table`.`outbound-cost-pal`) AS `average`
FROM `warehouse-cost-table`
) cost
;
你应该避免在函数名和方括号之间使用 space .. AVG()
我的简单 SQL 代码有问题,因为我只是一个初学者。 我正在尝试检索不同国家/地区的出站成本伙伴的平均值,并将其写入仓库的每个相应行。
我的代码如下:
INSERT INTO Warehouse (`inbound-cost-pal`)
SELECT cost.`average`
FROM Warehouse AS wh
INNER JOIN (
SELECT
AVG (`warehouse-cost-table`.`outbound-cost-pal`) AS `average`
FROM `warehouse-cost-table`
)cost
ON wh.`location-tariff-code` = LEFT(cost.`country`,2)
;
我收到错误消息“'on clause' 中的未知列 'cost.country'”。
您在子查询成本select中忘记了国家
INSERT INTO Warehouse (inbound-cost-pal)
SELECT cost.average
FROM Warehouse AS wh
INNER JOIN (
SELECT
AVG (warehouse-cost-table.outbound-cost-pal) AS average,
country
FROM warehouse-cost-table
)cost
ON wh.location-tariff-code = LEFT(cost.country,2)
;
如果您需要每个国家/地区的 avg(),那么您还需要子查询中的国家/地区
INSERT INTO Warehouse (`inbound-cost-pal`)
SELECT cost.`average`
FROM Warehouse AS wh
INNER JOIN (
SELECT `warehouse-cost-table`.`country`,
AVG(`warehouse-cost-table`.`outbound-cost-pal`) AS `average`
FROM `warehouse-cost-table`
GROUP BY `warehouse-cost-table`.`country`
) cost
ON wh.`location-tariff-code` = LEFT(cost.`country`,2)
;
否则,如果您不需要国家/地区的 avg() 而需要整体平均值,那么您可以使用交叉连接而不是内部连接
INSERT INTO Warehouse (`inbound-cost-pal`)
SELECT cost.`average`
FROM Warehouse AS wh
CROSS JOIN (
SELECT
AVG(`warehouse-cost-table`.`outbound-cost-pal`) AS `average`
FROM `warehouse-cost-table`
) cost
;
你应该避免在函数名和方括号之间使用 space .. AVG()