This question already has answers here:
MySQL Error 1093 - Can't specify target table for update in FROM clause
(16 answers)
Closed 4 years ago.
I am trying to simple update query with following syntax:
UPDATE cl
SET cl_s="active_"
WHERE id in (
SELECT distinct
c.id
from
cl c,
ac a,
cl_ac ac
where
c.id=ac.cl_id
and
a.id=ac.ac_id
and
c.cl_s="someval"
and
a.ac_no !=""
)
;
The error I get is: Error Code: 1093. You can't specify target table for update in FROM clause
I've read through posts which mention that MySQL is unable to run query like this. I'd want to know a seamless way to restructure such queries to a structure which works. The solution seems to be using joins instead but I can't seem to grasp the concept.
A pseudo code would be helpful, to help with the understanding. Something like
(Main query)
where var in (subquery generating var list)
to
(main query) inner join (subquery generating var list) on var ???
You can use JOIN in UPDATE statements in MySQL
UPDATE cl
JOIN cl_ac ON cl.id = cl_ac.cl_id
JOIN ac ON cl_ac.ac_id = ac.id
SET cl.cl_s = 'active_'
WHERE cl.cl_s = 'someval' AND ac.ac_no != ''
PS: Use single-quotes for string literals and date literals in SQL. Double-quotes may be used for delimited identifiers, depending on the sql_mode.
Related
This question already has answers here:
Can you use an alias in the WHERE clause in mysql?
(5 answers)
Closed 4 years ago.
I have a query that runs fine, when I try to add one more column to where clouses it cannot find the column and gives an error.
SELECT '1' AS `row_count`, (
SELECT
COUNT(*)
FROM
`attendances`
WHERE `program_sessions`.`id` = `attendances`.`program_session_id`
AND `attendances`.`deleted_at` IS NULL
) AS `attendances_count`
FROM
`program_sessions`
LEFT JOIN `programs` ON `programs`.`id` = `program_sessions`.`program_id`
LEFT JOIN `program_categories` ON `program_categories`.`id` = `programs`.`program_category_id`
LEFT JOIN `service_areas` ON `service_areas`.`id` = `program_categories`.`service_area_id`
LEFT JOIN `locations` ON `locations`.`id` = `programs`.`location_id`
WHERE (
LOWER(`program_categories`.`name`) LIKE "%3%" OR
LOWER(`programs`.`name`) LIKE "%3%" OR
LOWER(`locations`.`name`) LIKE "%3%" OR
(attendances_count = 3) OR
LOWER(`service_areas`.`name`) LIKE "%3%"
)
AND `program_sessions`.`deleted_at` IS NULL
MySQL said:
#1054 - Unknown column 'attendances_count' in 'where clause'
The query somehow cannot reach the attendances_count. What is it that I am doing wrong?
Found the problem here, aparently where clouse is not able to see the aliased columns. I should use having instead.
Can you use an alias in the WHERE clause in mysql?
This question already has answers here:
Deleting rows from SQLite table when no match exists in another table
(3 answers)
Closed 6 years ago.
I am developing an iOS and Android application using Xamarin Forms. I have the following code:
db2.Execute("DELETE w FROM Word AS w" +
" LEFT JOIN WordSource AS ws ON ws.WordId = w.WordId" +
" WHERE ws.WordId IS NULL");
But I kept getting syntax error near w. Can someone let me know what I'm doing wrong? Is DELETE... JOIN... query not supported in sqlite? If so, how can I accomplished this code in sqlite?
A simple way is based on an a in clause and subselect with left join
db2.Execute( "DELETE FROM Word WHERE id in (
SELECT id FROM Word as W
LEFT JOIN WordSource AS ws ON ws.WordId = w.WordId
WHERE ws.WordId IS NULL
)");
This question already has an answer here:
MySQL 5.7 error (1093: You can't specify target table ___ for update in FROM clause) - usual solution not working
(1 answer)
Closed 7 years ago.
I have this update statement in a trigger in one of my Databases:
UPDATE trainees
SET rsi_total = (
SELECT SUM(RsiTotal) as RsiTotal
FROM (
SELECT MAX(hours) as RsiTotal
FROM courses
LEFT JOIN do_not_add ON courses.fk_class_id = do_not_add.fk_class_id
INNER JOIN trainees ON courses.FK_TRAINEES_ID = trainees.PK_TRAINEE_ID
WHERE do_not_add.fk_class_id IS NULL
AND trainees.pk_trainee_id = new.fk_trainees_id
GROUP BY courses.FK_CLASS_ID
) courses
)
WHERE trainees.pk_trainee_id = new.fk_trainees_id
and does exactly what I expect, recently I migrated my database to another machine and moved and renamed a few tables and columns. So I re-wrote this trigger to:
UPDATE main
SET rsi_total = (
SELECT SUM(RsiTotal) as RsiTotal
FROM (
SELECT MAX(completed_hrs) as RsiTotal
FROM courses
LEFT JOIN jac.do_not_add ON courses.fk_class_id = do_not_add.fk_class_id
INNER JOIN main ON courses.fk_main_id = main.pk_main_id
WHERE do_not_add.fk_class_id IS NULL
AND main.pk_main_id = new.fk_main_id
GROUP BY courses.FK_CLASS_ID
) courses
)
WHERE main.pk_main_id = new.fk_main_id
But I get Error 1093? I looked up this error and it says I can not update a table that is being changed, what I dont understand is why does the old trigger continue to work but not this new?
Migrating to another machine, you probably got a more recent version of MySql, where the optimizer doesn't accept any longer subqueries to access the same table which is being updated.
Look at this detailed answer.
I know there is more than one question out there that matches this, but I am relatively new to mysql, and I can't seem to make this work using sub quests or the USING key word, plus I find the mysql on line docs a complete mystery.
I started trying to build my DELETE query using a SELECT query as my base and was able to get all the rows that I wanted to delete:
select *
from writings_tags_link
join writing_tags on writing_tags.id = writings_tags_link.tag_id
where writing_tags.tag = 'tag one'
and then just replaced select all with DELETE so:
delete
from writings_tags_link
join writing_tags on writing_tags.id = writings_tags_link.tag_id
where writing_tags.tag = 'tag one'
I gather from both the error message and from other similar posts that you can't use 'ON' to join tables in a delete query, you have to use USING or a sub query. The query I built with USING returns a really strange error, first the query:
DELETE
FROM writings_tags_link
USING writing_tags_link INNER JOIN writing_tags
WHERE writing_tags.id = writings_tags_link.tag_id
AND writing_tags.tag ='tag one'
error:
#1109 - Unknown table 'writings_tags_link' in MULTI DELETE
This table does exist, obviously, my original select query returned the desired results. Any help / explanation would be so very appreciated!
Please keep in mind, I'm only trying to delete the data in the linking table.
Your information is incorrect about requiring the use of the USING keyword in DELETE syntax when using JOINs - the documentation provides examples in the multi-delete section:
DELETE wtl
FROM WRITINGS_TAGS_LINK wtl
JOIN WRITING_TAGS wt ON wt.id = wtl.tag_id
WHERE wt.tag = 'tag one'
I have the following query in HQL:
update ProjectFile pf1
set pf1.validUntil.id =123
where pf1 = (
select pf from ProjectVersion pv, ProjectFile as pf
where pf.validFrom.sequence <= pv.sequence
and pf.validUntil.sequence >= pv.sequence
and pf.state <> 12
and pf.projectVersion.project.id = 1
and pv.project.id = 1
and pv.id = 12
and pf.id not in (2,3,4)
)
Hibernate parses the query correctly and generates SQL, but the database (MySQL) fails with error:
You can't specify target table 'ProjectFile' for update in FROM clause
The problem seems to be that the table to be updated is queried in the same context. Is there any way to rewrite the HQL query to produce SQL that can be executed in MySQL correctly? The other approach would be to create an intermediate table, which is what exactly I am trying to avoid.
I bumped into the same problem and posted a question here: MySQL/SQL: Update with correlated subquery from the updated table itself.
To solve your problem, you need to join at the UPDATE level, please take a look at the answer to my question.