Can't update a target table - mysql

I have two tables, one a options table (rot) that has the metrics of a set of rooms and another that has the current state of the rooms (rt). I want to have a daily event that updates the cost_to_date in the rt using the values stored in the rot.
When I tried the SQL:
UPDATE room_tbl SET COST_TO_DATE_rt = COST_TO_DATE_rt + (
SELECT PerDiem_rot FROM room_options_tbl, room_tbl
WHERE `ROOM_OPT_ID_rot` = `ROOM_OPT_ID_rt`
AND `ADULT_COUNT_rot` = `ADULT_COUNT_rt`)
I get the error: #1093 - You can't specify target table 'room_tbl' for update in FROM clause
My searching for a solution led me to try a temporary table using aliasing but my attempts at it have all resulted in syntax errors. Any help would be appreciated.

You can't use a query because its not supported by MySQL in an update clause
From 13.2.10. UPDATE Syntax
Currently, you cannot update a table and select from the same table in a subquery.
Instead try the following
UPDATE room_options_tbl, room_tbl
SET COST_TO_DATE_rt = COST_TO_DATE_rt + PerDiem_rot
WHERE `ROOM_OPT_ID_rot` = `ROOM_OPT_ID_rt`
AND `ADULT_COUNT_rot` = `ADULT_COUNT_rt`

Related

Selecting columns to insert into table with where condition

I am working on a project where I need to grab the img path from a backup table and insert it in to the image path column of a new table based on if the item names match up perfectly (as the id's vary).
This is what I tried, but getting an error that the items.prod_name column is not found:
MySQL
INSERT INTO items (img_path)
SELECT img_path
FROM items_backup
WHERE items.prod_name = items_backup.prod_name
You would seem to want an update, not an insert. The syntax in MySQL is:
update items i join
items_backup ib
on i.prod_name = ib.prod_name
set i.img_path = ib.img_path;
To follow up on my comment:
UPDATE items
INNER JOIN items_backup ON items.prod_name = items_backup.prod_name
SET items.img_path = items_backup.img_path

Update values in one table based on values in another table where a specific criteria matches

I have picked up the query from another thread posted here and have edited based on the tables I have. I get some syntax error when I try to save it. What am I doing wrong here?
UPDATE cleanedGC
SET cleanedGC.Competency = DomainMapTBL.[Person SMU Name]
From cleanedGC, DomainMapTBL
WHERE cleanedGC.[Person SMU #]=DomainMapTBL.[Person SMU]
Correct syntax would be:
UPDATE cleanedGC
INNER JOIN DomainMapTBL
ON cleanedGC.[Person SMU #]=DomainMapTBL.[Person SMU]
SET cleanedGC.Competency = DomainMapTBL.[Person SMU Name]

Select column to update based on value

What I am trying to do is reduce the time needed to aggregate data by producing a roll-up table of sorts. When I insert a record, an after insert trigger is fired which will update the correct row. I would update all of the columns of the roll-up table if I need to, but since there are 25 columns in the table and each insert will only update 2 of them, I would rather be able to dynamically select the columns to update. My current update statement in the after insert trigger looks similar to this:
update peek_at_chu.organization_data_state_log odsl
inner join ( select
lookUpID as org_data_lookup,
i.interval_id,
peek_at_chu.Get_Time_Durration_In_Interval1('s', new.start_time, new.end_time, i.start_time, i.end_time) as time_in_int,
new.phone_state_id
from
(peek_at_chu.interval_info i
join peek_at_chu.interval_step int_s on i.interval_step_id = int_s.interval_step_id)) as usl on odsl.org_date_lookup_id = usl.org_data_lookup
and odsl.interval_id = usl.interval_id
set
total_seconds = total_seconds + usl.time_in_int,
case new.phone_state_id
when 2 then
available_seconds = available_seconds + time_in_int
end;
In this, lookUpID is a variable previously declared in the trigger. The field that will dictate which field of the roll-up table to update is new.phone_state_id. The phone_state_id's are not consistent, that is some numbers are skipped in this table, so an update based on column number is out the window unless I create a mapping.
The case option throws an error but I am hoping to use something similar to that instead of 25 if statements if I can.
You have to update all the columns, but use a conditional to determine whether to give it a new value or keep the old value:
set total_seconds = total_seconds + usl.time_in_int,
available_seconds = IF(new.phone_state_id = 2, available_seconds + time_in_int, available_seconds)
Repeat the pattern in the last line for all the other columns that need to be updated conditionally.

Update using Select Statement

I wanna write a query like this :
UPDATE `test_credit`
SET `test_credit`.`credit`=(`test_credit`.`credit`-((`test_credit`.`credit`/100)*5))
WHERE `test_credit`.`name` = `users`.`uname`
in fact i want to get a query on users.uname = test_credit.name but mysql say it has error and realize users.uname as column
what is correct query ?
You need to explicitly join it with table users. As far from my understanding based on your query, you want to calculate the credit if the names exists on both tables.
Give this a try,
UPDATE test_credit a
INNER JOIN users b
ON a.name = b.uname
SET a.credit = (a.credit - ((a.credit/100) * 5.0))
-- WHERE b.parent= "example"

Update Value in Table from another table

I realized that i was using a varchar attribute as a index/key in a query, and that is killing my query performance. I am trying to look in my precienct table and get the integer ID, and then update my record in the household table with the new int FK, placed in a new column. this is the sql i have written thus far. but i am getting a
Error 1093 You can't specify target table 'voterfile_household' for update in FROM clause, and i am not sure how to fix it.
UPDATE voterfile_household
SET
PrecID = (SELECT voterfile_precienct.ID
FROM voterfile_precienct INNER JOIN voterfile_household
WHERE voterfile_precienct.PREC_ID = voterfile_household.Precnum);
Try:
update voterfile_household h, voterfile_precienct p
set h.PrecID = p.ID
where p.PREC_ID = h.Precnum
Take a look at update reference here.
Similarly, you can use inner join syntax as well.
update voterfile_household h inner join voterfile_precienct p on (h.Precnum = p.PREC_id)
set h.PrecID = p.ID
What if the subquery returns more than one result? That's why it doesn't work.
On SQL Server you can get this type of thing to work if the subquery does "SELECT TOP 1 ...", not sure if mysql will also accept it if you add a "limit 1" to the subquery.
I also think this is pretty much a duplicate of this question ("Can I have an inner SELECT inside of an SQL UPDATE?") from earlier today.
Firstly, your index on a varchar isn't always a bad thing, if it is not a key you can shrink how much of the field you index to only index say the first 10 chars or so.
Secondly, it won't let you do this as if it is a set that is returned it could break.