How to update based on anoter row in the same table? - mysql

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.

Related

Using group by in SET clause

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)

Updating MS Access table with a specific value based on another table

I'm trying to update a yes/no column in a table based on a lack of matches from a second table as follows:
UPDATE Table1
INNER JOIN Table2
ON (Table1.Date_last_action = Table2.MaxOfDate_last_action)
AND (Table1.Office = Table2.Office)
AND (Table1.Response_ID = Table2.Response_ID)
SET Table1.IsDeprectated = 1
WHERE (([Table2].[MaxOfDate_last_action] Is Null));
OR
UPDATE Table1
INNER JOIN Table2
ON (Table1.Date_last_action <> Table2.MaxOfDate_last_action)
AND (Table1.Office = Table2.Office)
AND (Table1.Response_ID = Table2.Response_ID)
SET Table1.IsDeprectated = 1;
Neither of which is working.
What I'm aiming for is that when the update query is executed that the IsDeprecated column is set to True for all rows in Table1 that DO NOT have a matching row in Table2
Is this possible?
[Edit following Tim's comment]
You could use exists logic along with a subquery:
UPDATE Table1 t1
SET t1.IsDeprectated = 1
WHERE NOT EXISTS (SELECT 1 FROM Table2 t2
WHERE t1.Date_last_action <> t2.MaxOfDate_last_action AND
t1.Office = t2.Office AND
t1.Response_ID = t2.Response_ID);

Update field set to value of row in same field

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.

updating a column in table 1 by joining information from table 2

I have tabel 1 that has VIN which i want to update.
table 1 and table 2 has OBJ ID and POID (respectively) which are same.
I only know device ID which is present in table 2.
update table.1 set VIN = '5TDKK3DC6BS018229'
from table 2, table 1
where 2.device ID = 'TCAXLcKkt3'
and 2.OBJ = 1.POID;
I am getting SQL command not properly ended.
Make sure you remove the semi-colon at end of the query since some databases will complain if its there in TOAD.
If you are using SQL Server then following query will work.
UPDATE table1
SET VIN = '5TDKK3DC6BS018229'
FROM table1
INNER JOIN table2 ON table1.POID = table2.OBJ
WHERE table2.deviceID = 'TCAXLcKkt3'
If you want the same query for MySQL, then use the one below.
UPDATE table1 a
JOIN table2 b
ON a.poid = b.obj
SET a.vin = '5TDKK3DC6BS018229'
WHERE b.deviceid = 'TCAXLcKkt3'
If you are using Oracle, then use the query as below.
UPDATE table1
SET table1.vin = '5TDKK3DC6BS018229'
where exists (SELECT table2.obj
FROM table2
WHERE table2.obj = table1.poid
AND table2.deviceid = 'TCAXLcKkt3')
Error message looks like you are using Oracle.
If am not wrong this is what you are looking for
UPDATE table1
SET vin = '5TDKK3DC6BS018229'
WHERE EXISTS (SELECT 1
FROM table2 B
WHERE table1.obj = B.poid
AND B."device id" = 'TCAXLcKkt3')
In Oracle to update table from another table using Join try the below syntax
UPDATE
(SELECT A.VIN
FROM table1 A
INNER JOIN table2 B
ON A.OBJ = B.POID
WHERE B."device ID" = 'TCAXLcKkt3'
) t
SET T.VIN = '5TDKK3DC6BS018229'

update query in sql based on condition fro two tables

I need to build a update query. My existing code looks like this.
update table1
set data_plan=(select d.data_plan from table1 m,table2 d
where m.msidn = d.msidn and m.data_plan!=d.data_plan);
table 1 has columns msisdn and data_plan, table 2 also has same columns. I want to update the table1 data_plan column depending on some condition which I get through select query. But when I run the code I get this error.
You can't specify target table 'msisdn1' for update in FROM clause
Try it this way
update table1 m join table2 d
on m.msidn = d.msidn
and m.data_plan != d.data_plan
set m.data_plan = d.data_plan
Try this ...
UPDATE table1 SET m.data_plan=d.data_plan
FROM table1 m
INNER JOIN table2 d ON m.msidn = d.msidn and m.data_plan!=d.data_plan