UPDATE FROM WHERE Subquery SQL Server - mysql

How can I use a subquery from an UPDATE statement? Here is my query:
UPDATE car_availability
SET availability_status = 'GOOD'
FROM car_trip AS TRIP
WHERE car_availability.car_no = TRIP.car_no
AND NOT EXISTS (SELECT DISTINCT A.car_No
FROM car_maintenance A
WHERE A.car_no = TRIP.vehicle_id
AND start_date = TRIP.workday)
AND EXISTS (SELECT DISTINCT B.car_No
FROM car_maintenance B
WHERE B.car_no = TRIP.vehicle_id
AND end_date IS NOT NULL)
I get an error 'FROM clause in UPDATE and DELETE statements cannot contain subquery sources or joins.'
I want to update availability_status to GOOD in which car exists in car_maintenance with its start_date is equal to workday of car_trip and cars which is in car_maintenance with its end date is not null.

As #McNets said, you need to add the car_availability table to your FROM clause. Try this:
UPDATE CA
SET availability_status = 'GOOD'
FROM car_availability CA
JOIN car_trip AS TRIP ON CA.car_no = TRIP.car_no
WHERE NOT EXISTS (SELECT DISTINCT A.car_No
FROM car_maintenance A
WHERE A.car_no = TRIP.vehicle_id
AND start_date = TRIP.workday)
AND EXISTS (SELECT DISTINCT B.car_No
FROM car_maintenance B
WHERE B.car_no = TRIP.vehicle_id
AND end_date IS NOT NULL)

Related

How to update a column by running max() on the same column?

I have three rows of data:
temp_number tempdate
A12345 null
A12345001 '2018-01-01'
A12345002 '2018-01-02'
I want to set tempdate on A12345 to 2018-01-02 using this query:
update table_a1 set tempdate = (select max(tempdate) from table_a1 where
substr(temp_number,1,6) = 'A12345')
where temp_number = 'A12345'
The query above is not working, and I want to update the value by using a max() function rather than giving any actual value.
Your WHERE clause is wrong, and you should be updating only NULL records:
UPDATE table_a1
SET tempdate = (SELECT MAX(tempdate)
FROM table_a1
WHERE temp_number LIKE 'A12345%')
WHERE temp_number = 'A1234' AND tempdate IS NULL;
you could use a join on subquery for max value
update table_a1 m
INNER JOIN (
select max(tempdate) max_date, substr(temp_number,1,6) temp1
from table_a1
where substr(temp_number,1,6) = 'A12345')
group by temp
) t on t.temp1 = m.tempdate
set tempdate = t.max_date

SQL Subquery return more than 1 row

I need to set "dph" in this table "Strobjednavka", but i donĀ“t know whats wrong there. Please help :).
Here is my SQL script:
UPDATE STRObjednavka as o SET dph = (
SELECT dph FROM STRCena WHERE
menuKodCode =
(SELECT menuKodCode FROM STRMenu WHERE
id = o.menuId
)
AND
skupinaId =
(SELECT stravGroupId FROM grups1 WHERE
PKey =
(SELECT SGroup FROM users1 WHERE
PKey = o.userId
)))
WHERE o.price > 0 AND `date` > '2015-01-28 13:52:36' AND dph = 0;
SQL say : SQL error 1242: Subquery returns more than 1 row
You can able to update with below script, but you need to check whether update is correct or not, If you give some sample data then it will be easy to track the problem.
UPDATE STRObjednavka as o SET dph = (
SELECT max(dph) FROM STRCena WHERE
menuKodCode =
(SELECT max(menuKodCode) FROM STRMenu WHERE
id = o.menuId
)
AND
skupinaId =
(SELECT max(stravGroupId) FROM grups1 WHERE
PKey =
(SELECT max(SGroup) FROM users1 WHERE
PKey = o.userId
)))
WHERE o.price > 0 AND `date` > '2015-01-28 13:52:36' AND dph = 0;
Unfortunately, MySQL doesn't allow you to LIMIT a subquery. Depending on your use case you can add MIN or MAX to your subqueries. Here it is with MINs in all the subqueries:
UPDATE STRObjednavka as o SET dph = (
SELECT MIN(dph) FROM STRCena WHERE
menuKodCode =
(SELECT MIN(menuKodCode) FROM STRMenu WHERE
id = o.menuId
)
AND
skupinaId =
(SELECT MIN(stravGroupId) FROM grups1 WHERE
PKey =
(SELECT MIN(SGroup) FROM users1 WHERE
PKey = o.userId
)))
WHERE o.price > 0 AND `date` > '2015-01-28 13:52:36' AND dph = 0;
Although you really only need to add it to the subquery that's returning more than one row.
Your first problem is that you're writing '.... = (SELECT .... )'. Since you're using the equality operator, you're asking SQL to assign an entire column of values to a single cell. Change your equality operators before your subqueries to IN operators.
You probably should use a different query pattern.
You have this sort of thing in your query, in several places.
WHERE menuKodCode = /* !! might generate error 1242 */
(SELECT menuKodCode FROM STRMenu WHERE id = o.menuId)
There's no guarantee that your inner query won't return more than one row, and when it does, MySQL throws error 1242.
SQL works wiith sets of values. If you used IN instead of =, your query would work.
WHERE ... menuKodCode IN
(SELECT menuKodCode FROM STRMenu WHERE id = o.menuId)
But you should figure out whether that logic is correct. If I were you I'd do a whole bunch of SELECT operations to test it before doing UPDATE.

MYSQL SELECT is slow when crossing multiple tables

I have a mysql query which is to return the only 1 record that need to cross multiple table. However, the mysql query is slow when executing.
Query:
SELECT *,
(SELECT TreeName FROM sys_tree WHERE TreeId = Mktg_Unit_Booking.ProjectLevelId) AS PhaseName,
(CASE WHEN ProductType = 'U' THEN (SELECT UnitNo FROM prop_unit pu WHERE pu.UnitId = mktg_unit_booking.UnitId)
ELSE (SELECT BayNo FROM prop_car_park pcp WHERE pcp.CarParkId = UnitId) END) AS UnitNo,
(SELECT CustomerName FROM mktg_customer mc WHERE mc.CustomerId = mktg_unit_booking.CustomerId) AS CustomerName
FROM Mktg_Unit_Booking
WHERE IsDeleted <> '1' AND IsApproved = '1'
AND UnitId = 1110 AND ProductType = 'U'
ORDER BY UnitNo
I have run EXPLAIN in the query and I got this:
Any other suggestion on how to improve the speed of the query?
Thank you!
you are doing the cross product, instead of that you should use join.
Don't use sub-queries in select statement instead use proper join on Mktg_Unit_Booking in after from statement.
you query should something look like :
select
sys_tree.TreeName AS PhaseName,
case
WHEN Mktg_Unit_Booking.ProductType = 'U' then prop_unit.UnitNo
else prop_car_park.BayNo
end as UnitNo,
mktg_customer.CustomerName AS CustomerName
FROM Mktg_Unit_Booking
left join sys_tree on sys_tree.TreeId = Mktg_Unit_Booking.ProjectLevelId
left join prop_unit on prop_unit.UnitId = Mktg_Unit_Booking.UnitId
left join prop_car_park on prop_car_park.CarParkId = Mktg_Unit_Booking.UnitId
left join mktg_customer on mktg_customer.CustomerId = Mktg_Unit_Booking.CustomerId
WHERE IsDeleted <> '1' AND IsApproved = '1'
AND UnitId = 1110 AND ProductType = 'U'
ORDER BY UnitNo;
I have assumed that each table consists of only 1 matching tuple. If there are more then your logic needs to be modified.

mysql update statement using the from clause on the same table

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)

update table with data from multiple tables and group by month -

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)