gpt4 book ai didi

mysql - 从选择插入失败并出现奇怪的错误

转载 作者:行者123 更新时间:2023-11-29 15:41:03 25 4
gpt4 key购买 nike

我有一个插入后触发器,它将从选择中获取数据,这将返回错误

列计数与第 1 行的值计数不匹配 SQL1.sql 1 1

已尝试手动运行此命令 - 相同的结果

  INSERT INTO stkmonth (MONTH, YEAR, LOCATION, ATTRIB1, ATTRIB2, ATTRIB3, QTY_ONHAND, COST)
VALUES ((SELECT
MONTH(new.TRANS_DATE) AS month,
YEAR(new.TRANS_DATE) AS year,
new.location AS location, stock.ATTRIB1, stock.ATTRIB2, stock.ATTRIB3,
SUM(stocklvl.QTY_ONHAND) AS qty_onhand,
SUM(stocklvl.QTY_ONHAND * stocklvl.AVG_COST) AS cost
FROM sales INNER JOIN stocklvl ON sales.ISC_CODE = stocklvl.ISC_CODE
INNER JOIN stock ON sales.ISC_CODE = stock.ISC_CODE WHERE stock.ISC_CODE = new.isc_code GROUP BY stocklvl.ISC_CODE))
ON DUPLICATE KEY UPDATE
qty_onhand = qty_onhand - new.qty, cost = (new.qty * new.cost);

我希望这能起作用,但似乎认为存在列错误。请注意,有时可能找不到任何内容来选择,这没关系,在这种情况下我希望它继续前进,空记录集可能是问题所在吗?

最佳答案

引用:MySQL Insert...Select Syntax

  • Insert..Select 中,请勿使用 VALUES 关键字。
  • 不要将 select 语句括在方括号中。
INSERT INTO stkmonth (MONTH, YEAR, LOCATION, ATTRIB1, ATTRIB2, ATTRIB3, QTY_ONHAND, COST)
SELECT
MONTH(new.TRANS_DATE) AS MONTH,
YEAR(new.TRANS_DATE) AS YEAR,
new.location AS location,
stock.ATTRIB1,
stock.ATTRIB2,
stock.ATTRIB3,
SUM(stocklvl.QTY_ONHAND) AS qty_onhand,
SUM(stocklvl.QTY_ONHAND * stocklvl.AVG_COST) AS cost
FROM sales
INNER JOIN stocklvl ON sales.ISC_CODE = stocklvl.ISC_CODE
INNER JOIN stock ON sales.ISC_CODE = stock.ISC_CODE
WHERE stock.ISC_CODE = new.isc_code
GROUP BY stocklvl.ISC_CODE
ON DUPLICATE KEY UPDATE qty_onhand = qty_onhand - new.qty, cost = (new.qty * new.cost);

关于mysql - 从选择插入失败并出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57650594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com