I'm trying to update a specific field and set it equal to value of a row in the same field.
What have I tried so far is this:
mysql> UPDATE tblitem SET imagefilename = (SELECT imagefilename from tblitem where itemid=2) where itemid=1'
1093 - You can't specify target table 'tblitem' for update in from clause
What I am trying to do here is to update the value of itemid 1 to the value of itemid 2.
Is that even possible? Thank you in advance.
In MySQL, if you're doing an UPDATE/INSERT/DELETE queries on a table, you can't reference the said table in the inner query. One workaround is to use a subquery inside the inner query:
UPDATE tblitem
SET imagefilename =
(
SELECT imagefilename
FROM (SELECT * FROM tblitem) AS t
WHERE itemid = 2
)
WHERE itemid = 1
Use a join instead:
UPDATE tblitem t JOIN
(SELECT imagefilename from tblitem where itemid = 2
) t2
SET t.imagefilename = t2.imagefilename
WHERE itemid = 1;
The SQL standard and other databases allow you to refer to the table being updated elsewhere in the update statement. However, MySQL does not allow this. The JOIN is a simple enough work-around.
Related
I'm trying to update a column of a table so that is equal to the count of something in another table. Like this:
UPDATE TABLE
SET TOTAL = (SELECT COUNT(f1)
FROM TABLE2
GROUP BY f2);
But I keep getting sub query returns more than 1 row, and I can't think of how to fix it.
UPDATE (copied from the comment)
f2 is the relation between TABLE and TABLE2 – Thomasd d
Based on your comment
f2 is the relation between TABLE and TABLE2
you probably want something like this
UPDATE TABLE T1, (SELECT f2, COUNT(F1) cnt FROM TABLE2 GROUP BY f2) T2
SET T1.TOTAL = T2.cnt
WHERE T1.f2=T2.f2
adapt T1.f2 if necessary
UPDATE t1
SET total = ( SELECT COUNT(f1)
FROM t2
WHERE t1.f2 = t2.f2 );
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=91de17deff657f66fa54b42fe20ed3c5
Add WHERE total IS NULL if you do not need to recalculate values for rows which have a value already.
Your subquery is returning multiple values and your SET statement is only expecting one. This might fix your code if that is what you are looking for.
UPDATE TABLE
SET TOTAL = (SELECT COUNT(f1)
FROM TABLE2)
I have a table in with a unique id column, a non-unique name column, and other irrelevant columns. I want to update rows of data based off a single id. I've tried:
UPDATE mytable SET myfield = 'blah' WHERE name IN (SELECT name FROM mytable WHERE id = 1613)
But that gives this error Table 'mytable' is specified twice, both as a target for 'UPDATE' and as a separate source for data
Then I tried:
UPDATE mytable SET myfield = 'blah' WHERE name IN (SELECT name WHERE id = 1613)
The problem with this is that it is only affecting 1 row, even though there are multiple rows that have the same name.
I know how to accomplish this by using 2 different queries. One to fetch the name, and the other to update rows using that name. But is there a way to do this with just 1 query?
You can join tables to update them :
UPDATE mytable t1 inner join mytable t2 on t1.name = t2.name SET t1.myfield = 'blah' where t2.id = 1613
When you try to update the same table as you a re getting the right amount of rows, the result set can change every row that is updated. that is not possible.
solution make a temporary table as below
UPDATE mytable SET myfield = 'blah' WHERE name IN (SELECT name FROM (SELECT * FROM mytable) t1 WHERE id = 1613)
I need to update a list of records coming from a SELECT statement I'm trying with something of this kind but I got errors regarding the statement format:
UPDATE
noleggio_veicoli
SET
data_esportazione = CURDATE()
WHERE id IN
(SELECT id FROM noleggio_veicoli WHERE id_convenzionato = 3);
But it just returns:
Error Code: 1093. You can't specify target table 'noleggio_veicoli'
for update in FROM clause
It looks like MySQL is only able to run classic UPDATE i.e.:
UPDATE
noleggio_veicoli
SET
data_esportazione = CURDATE()
WHERE
id_convenzionato = 3;
Unlucky this's not what I need since SELECT query I need to chain to the UPDATE statement is more complex and depends on Views already defined in the SQL server.
Like #Maxim said, you should do something like
CREATE TEMPORARY TABLE IF NOT EXISTS temp AS (
SELECT id FROM noleggio_veicoli where id_convenzionato=3
);
update noleggio_veicoli set data_esportazione=curdate()
where id in (select id from temp);
If you want to update data that match a select p.id from parent p inner join child c on c.pid=p.id left join child2 c2 on c.x=c2.y where c.xx=yy and p.x=y... you can do something like:
update parent p
set aaa=bbbb
from child c
left join child2 c2 on c.x=c2.y
where c.pid=p.id and c.xx=yy and p.x=y ...
You can specify this with a join:
UPDATE noleggio_veicoli nv JOIN
noleggio_veicoli nv3
ON nv.id = nv3.id and nvid3.id_convenzionato = 3
SET data_esportazione = curdate();
For some odd reason MySql doesn't allow to reference the same table you update in a first level subquery.
But you can work around that via a subqueryinception. (It's not a real word, probably)
I.e. using a sub-query in the sub-query.
Example using an IN:
UPDATE noleggio_veicoli
SET data_esportazione = current_date
WHERE id IN (select id from (select id from noleggio_veicoli where id_convenzionato = 3) q);
It works also with an EXISTS:
UPDATE noleggio_veicoli as t
SET data_esportazione = current_date
WHERE EXISTS
(
select 1
from (select id, id_convenzionato from noleggio_veicoli) as t2
where t2.id_convenzionato = 2
and t2.id = t.id
);
You can test it here on rextester
You can't update and read from one table at the same time. At first you must
select necessary ids and save them to somewhere, for example to temporary table
I want to update a certain record of a table only if certain another record does not already exist in the table.
I tried a SQL similar to following.
update mytable
set val = 'someval'
where id = 'someid' and
0 = (select count(*) from mytable where col='val2');
This fails with following error.
You can't specify target table 'mytable' for update in FROM clause
Only one process is updating this table, so preserving the atomicity of the operation is not necessary.
I know I can do this using two SQL queries, but is there a way to do this in a single query?
Because you are referring to the same table, the best way to do this uses LEFT JOIN:
update mytable t left join
mytable t2
on t2.col = 'val2'
set val = 'someval'
where t.id = 'someid' and t2.col is null;
There are several ways to do this. Here's one option using a subquery with not exists:
update mytable
set val = 'someval'
where id = 'someid'
and not exists (
select 1
from (select * from mytable) t
where col = 'val2')
SQL Fiddle Demo
Using the subquery bypasses the error you are receiving. Other approaches include outer join with null checks or using not in -- depends a bit on the data.
TRY this:
UPDATE mytable SET val = 'someval'
WHERE id = 'someid' AND col <> 'val2'
I want to create a query that updates an int based on the int of the row with an id that is 1 higher.
I have tried this query, but it says that i can't label the table in an update statement. But how do i reference it in my subquery?
update t1 a set `int1` = (select `int1` from t1 b where b.id=a.id+1);
How can I overcome that I can't use an alias?
Try this one -
UPDATE
t1 a
JOIN t1 b
ON b.id = a.id + 1
SET
a.int1 = b.int1;
If there are holes in id values, the query may be changed.