Mysql 更新 select 语句

标签 mysql

我正在尝试对以下 select 语句运行更新,但我认为 mysql 不太喜欢我的语法,请告知?

    UPDATE `invoice_lines`
SET 
`invoice_lines`.`cost_price` = Costnew,
`invoice_lines`.`list_price` = Listnew,
`invoice_lines`.`unit_price` = Unitnew
SELECT (quote_lines.`list_price` * products.commision_pers/100) AS Listnew, 
(quote_lines.`cost_price` * products.commision_pers/100) AS Costnew,
(quote_lines.`unit_price` * products.commision_pers/100) AS Unitnew
FROM `quote_lines`
INNER JOIN quotes
ON quotes.id = quote_lines.`quote_id`
INNER JOIN QuotePers
ON quoteid = quotes.id
INNER JOIN products
ON products.id = quote_lines.`related_id`
INNER JOIN invoice
ON invoice.`from_quote_id` = QuotePers.quoteid
INNER JOIN invoice_lines
ON invoice_lines.`invoice_id` = invoice.id
WHERE products.id = invoice_lines.`related_id`
AND  prodid = invoice_lines.related_id
AND invoice_id = invoice.id
GROUP BY  quotes.id,products.`id`

最佳答案

兄弟,尝试在 where 条件中引用这些列所在的表名称,下次在使用如此大的查询时需要使用 table_name 别名:)

UPDATE invoice_lines
SET 
invoice_lines.cost_price = Costnew,
invoice_lines.list_price = Listnew,
invoice_lines.unit_price = Unitnew
SELECT (quote_lines.list_price * products.commision_pers/100) AS Listnew, 
(quote_lines.cost_price * products.commision_pers/100) AS Costnew,
(quote_lines.unit_price * products.commision_pers/100) AS Unitnew
FROM quote_lines
INNER JOIN quotes
ON quotes.id = quote_lines.quote_id
INNER JOIN QuotePers
ON quote_lines.quoteid = quotes.id
INNER JOIN products
ON products.id = quote_lines.`related_id`
INNER JOIN invoice
ON invoice.from_quote_id = QuotePers.quoteid
INNER JOIN invoice_lines
ON invoice_lines.invoice_id = invoice.id
WHERE products.id = invoice_lines.related_id
AND quote_lines.prodid = invoice_lines.related_id
AND quote_lines.invoice_id = invoice.id
GROUP BY  quotes.id,products.id

关于Mysql 更新 select 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29092418/

相关文章:

MySQL : How to query two tables sequentially?

javascript - 链接通过 Ajax 拉入的 2 个不同 php 文件的结果

MySQL授予数据库除一张表之外的所有权限

mysql - 如何在连接中转换这个子查询?

mysql - 嵌套 SQL 查询和更新

mysql - nodejs mysql - 如何在现实项目中实现 pooledConnection?

mysql - phpmyadmin 错误

php - SQL 查询值未传递给 PHP 对象

mysql sql select和inner join for follower/followee names

java - hibernate 查询语法异常