i tried to run this query:
WITH updateables AS (
SELECT id_product, quantity
FROM products_order
WHERE id_order = 1239 AND state = 10
)
UPDATE product
SET product.stock = updateables.quantity
WHERE product.id_product = updateables.id_product```
But this trows:
Error Code: 1054. Unknown column 'updateables.id_product' in 'where clause```
Defining a CTE using the WITH clause does not implicitly join that CTE to other tables in your query.
You would need to use a JOIN in your query to the CTE:
WITH updateables AS (
SELECT id_product, quantity
FROM products_order
WHERE id_order = 1239 AND state = 10
)
UPDATE product JOIN updateables ON product.id_product = updateables.id_product
SET product.stock = updateables.quantity;
Related
I am trying to update two columns within a table from a select statment in MySQL 5.7.
The error I get is "invalid use of group function"
Stmt:
UPDATE
catalog mpc
JOIN
reviews mpr ON mpr.merchant_id = mpc.MERCHANT_ID and mpr.sku = mpc.ARTICLE_ID
SET
mpc.RATING = avg(mpr.rating),
mpc.RATINGS = count(mpr.rating)
WHERE
mpr.MERCHANT_ID = 1
AND mpr.sku = '133';
It looks about right to me, what could be the problem here?
You must aggregate first in reviews and then join to catalog:
UPDATE catalog mpc
INNER JOIN (
SELECT merchant_id, sku, AVG(rating) avg_rating, COUNT(rating) count_rating
FROM reviews
WHERE merchant_id = 1 AND sku = '133'
GROUP BY merchant_id, sku
) mpr ON mpr.merchant_id = mpc.MERCHANT_ID and mpr.sku = mpc.ARTICLE_ID
SET mpc.RATING = mpr.avg_rating,
mpc.RATINGS = mpr.count_rating
I have this mysql query:
select `sc_documentos`.*, `sc_clientesproveedores`.`nombre` as `cliente`,
`sc_documentos`.`descripcion` as `desc`, SUM(sc_documentos.total) AS sumaTotal,
`sc_series`.`nombre` as `serie`
from `sc_documentos`
inner join `sc_clientesproveedores` on `sc_clientesproveedores`.`id` = `sc_documentos`.`idCliente`
inner join `sc_series` on `sc_series`.`id` = `sc_documentos`.`idSerie`
where (`sumaTotal` >= 100 and `sumaTotal` <= 200 and `tipo` like '%presupuesto%'
and `sc_documentos`.`idUsuario` = 1682)
and `sc_documentos`.`deleted_at` is null order by `sc_documentos`.`created_at` desc
Mysql says: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sumaTotal' in 'where clause'
Please help I think that I can use more than one select o some, but don't know how...
In your query sumaTotal is an alias of SUM(sd_documentos.total). So either use HAVING sumaTotal >= 100 and sumaTotal <= 200 or WHERE SUM(sc_documentos.total) >= 100 AND SUM(sc_documentos.total) <= 200
sumaTotal is an alias of SUM part in your query so you can change the query like that:
select `sc_documentos`.*, `sc_clientesproveedores`.`nombre` as `cliente`,
`sc_documentos`.`descripcion` as `desc`,
`sc_series`.`nombre` as `serie`
from `sc_documentos`
inner join `sc_clientesproveedores` on `sc_clientesproveedores`.`id` = `sc_documentos`.`idCliente`
inner join `sc_series` on `sc_series`.`id` = `sc_documentos`.`idSerie`
HAVING (SUM(sc_documentos.total) >= 100 and SUM(sc_documentos.total) <= 200 and `tipo` like '%presupuesto%'
and `sc_documentos`.`idUsuario` = 1682)
and `sc_documentos`.`deleted_at` is null order by `sc_documentos`.`created_at` desc
This runs after simialbi instructions: (thanks again)
select `sc_documentos`.*, `sc_clientesproveedores`.`nombre` as `cliente`,
`sc_documentos`.`descripcion` as `desc`, SUM(sc_documentos.total) AS sumaTotal,
`sc_series`.`nombre` as `serie` from `sc_documentos`
inner join `sc_clientesproveedores` on `sc_clientesproveedores`.`id` = `sc_documentos`.`idCliente`
inner join `sc_series` on `sc_series`.`id` = `sc_documentos`.`idSerie`
where (`tipo` like '%presupuesto%' and `sc_documentos`.`idUsuario` = 1682) and `sc_documentos`.`deleted_at` is null
group by `idCliente` having sumaTotal>=600 and sumaTotal<=800
order by `sc_documentos`.`created_at` desc
The following is the MS Sql server Update statement
Update
HC_TranDetails
SET
InsPayments = (SELECT IsNull(SUM(ISNULL(CreditAmount,0)),0) From HC_TranDetails TDS
Where TDS.TransactionType = 2
AND TDS.ClaimNo = TD.ClaimNo
AND TDS.LineItemNo = TD.LineItemNo
AND IsNull(TDS.InsPlanRowID,'') <> ''
AND TDS.ReverseEntry <> 1 ),
Adjustments = (SELECT IsNull(SUM(ISNULL(CreditAmount,0)),0) From HC_TranDetails TDS
Where TDS.TransactionType = 8
AND TDS.ClaimNo = TD.ClaimNo
AND TDS.LineItemNo = TD.LineItemNo
AND IsNull(TDS.InsPlanRowID,'') <> ''
AND TDS.ReverseEntry <> 1 ),
FROM
HC_TranDetails TD
Now i am trying the same kind of statement in mysql as follows
UPDATE claimdetails SET balanceAmount = (SELECT IFNULL(SUM(IFNULL(debitamount,0)) - SUM(IFNULL(creditamount,0)),0)
FROM claimdetail CD WHERE CD.claimID = CDS.claimID)
FROM ClaimDetail CDS
But it is showing as syntax Error near 'From ClaimDetail CDS' at line 4
MySQL is squeamish about updates on the same table. The easy fix is to include an extra level of subquery. The proper fix, though, is to use a join
UPDATE claimdetails join
(select claimid, IFNULL(SUM(IFNULL(debitamount,0)) - SUM(IFNULL(creditamount,0)),0) as val
from ClaimDetails
group by claimid
) agg
on claimdetails.claimid = agg.claimid
SET balanceAmount = agg.val;
You can join the table you want to update with a subquery that calculates the balance for each claimid on the other table.
By using LEFT JOIN, it will update all records on table claimdetails. A value of 0 will be updated to any non existent claimid on the subquery.
UPDATE claimdetails a
LEFT JOIN
(
SELECT claimID,
SUM(IFNULL(debitamount, 0)) - SUM(IFNULL(creditamount,0)) bal
FROM claimdetail
GROUP BY claimID
) b ON a.claimID = b.claimID
SET a.balanceAmount = IFNULL(b.bal, 0)
i am a beginner and need some help on this. trying to update a table with data from multiple tables and grouping them in months or weeks
update monthly_report
SET De_Ative = (SELECT IFNULL(sum(statuschange.NUM_STATUS_CHANGES),0) FROM statuschange
WHERE month(STR_TO_DATE(statuschange.Evaluation_day,'%d.%m.%Y')) = monthly_report.Mon
AND statuschange.STATUS_CHANGE ='CANCEL' OR statuschange.STATUS_CHANGE ='LOCK'
group by month(STR_TO_DATE(statuschange.Evaluation_day,'%d.%m.%Y')))
[SQL] update monthly_report
SET De_Ative = (SELECT IFNULL(sum(statuschange.NUM_STATUS_CHANGES),0) FROM statuschange
WHERE month(STR_TO_DATE(statuschange.Evaluation_day,'%d.%m.%Y')) = monthly_report.Mon
AND statuschange.STATUS_CHANGE ='CANCEL' OR statuschange.STATUS_CHANGE ='LOCK'
group by month(STR_TO_DATE(statuschange.Evaluation_day,'%d.%m.%Y')))
-- group by monthly_report.Mon
[Err] 1242 - Subquery returns more than 1 row
Try this one,
UPDATE monthly_report a
LEFT JOIN
(
SELECT MONTH(STR_TO_DATE(a.Evaluation_day,'%d.%m.%Y')) mnt,
SUM(a.NUM_STATUS_CHANGES) totalSum
FROM statuschange a
WHERE a.STATUS_CHANGE IN ('CANCEL', 'LOCK')
GROUP BY MONTH(STR_TO_DATE(a.Evaluation_day,'%d.%m.%Y'))
) b ON b.mnt = a.mon
SET a.De_Ative = IFNULL(b.totalSum, 0)
I have two tables: o_daily_lcsgeneration and o_daily_generation.
While trying to update the o_daily_generation I receive an error saying:
error(1111) invalid use of Group function
Here is the code I am running:
update o_daily_generation join o_daily_lcsgeneration
on o_daily_generation.Location =o_daily_lcsgeneration.Location
and o_daily_generation.Date =o_daily_lcsgeneration.Date
set o_daily_lcsgeneration.Turbine_Generation =sum(o_daily_generation.Turbine_Generation)
Try this instead:
UPDATE o_daily_generation od
INNER JOIN
(
SELECT Location, SUM(Turbine_Generation) TurbineSum
FROM o_daily_lcsgeneration
GROUP BY Location
) og ON od.Location = og.Location
SET od.Turbine_Generation = og.TurbineSum
I did update the above query based on above answer but there is an issue.
UPDATE tmpTotal t1
INNER JOIN
(
SELECT thirdlevel_delivery_id, MAX(baseline_id) MaxBaseLineId
FROM tmpTotal
GROUP BY release_id
) t2 ON t1.release_id = t2.release_id
SET t1.planned_delivery_date = t2.MaxBaseLineId;
It gives
Error Code : 1054
Unknown column 't2.release_id' in 'on clause'