Update statement in Mysql and Oracle - mysql

I have an update statement that is working in Mysql.(update multiple records, based on the column in another table)
UPDATE `table1` m
INNER JOIN
(SELECT cse_cd FROM table2 WHERE clsf_ind='NC') t
ON m.cse_cd = t.cse_cd
SET m.ST_CD = 'QUEST03'
However, it does not work in Oracle.
Can somebody help on this.

This should do the job:
UPDATE table1
SET ST_CD = 'QUEST03'
WHERE EXISTS
(SELECT 1
FROM table2
WHERE clsf_ind='NC' AND table1.cse_cd=table2.cse_cd);

Related

MySQL: No update statements working

I'm trying to update a table of my database with values from another table and database, using a common field.
However none of my statements are working and I can't figure out why.
They are running for ten minutes (way too long) and then the server disconnects.
First try:
UPDATE database1.table1 t1, database2.table2 t2
SET t1.field1 = t2.field1
WHERE t1.field2 = t2.field2;
Second try:
UPDATE database1.table1 t1
INNER JOIN database2.table2 t2
ON t1.field2 = t2.field2
SET t1.field1 = t2.field1;
Can somebody push me in the right direction?
The WHERE/ON-conditions are working in separate select statements. As a workaround I created new tables with a SELECT JOIN, but that is very slow work.
field1 is unique in table2, but not table1. I want to update multiple entries in table1 with a unique value from table2.

mysql - update a field in 1 table where another field in that table = a field in another table

Need help with a mysql query. I have 2 tables - table1 and table2. I am trying to update a field in table1 which is not included in table2 - named Status. And I want to update that field with the value of 'A'. table1 and table2 DO have a field in common - named Member_ID. Here is my query that is giving me errors:
UPDATE table1 SET Status='A' WHERE Member_ID=table2.Member_ID;
Is there some type of join that is needed? Any help would be appreciated. Thanks.
cdr6800
This can be done with exists
UPDATE table1 s
SET s.status = 'A'
WHERE EXISTS(select 1 from table2 t
WHERE t.member_id = s.member_id)
You have to JOIN table1 to table2 like this:
UPDATE table1 AS t1
INNER JOIN table2 AS t2 ON t1.Member_ID = t2.Member_ID
SET t1.Status='A'
This will update all table1 records having a Member_ID value that also exists in table2.

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

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

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.