update query in sql based on condition fro two tables - mysql

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

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)

Apply code on every row of a result of a SELECT statement

I need to SELECT FROM a mysql database and the result can be 0 or more rows.
For a certain column and for every row I need to do an UPDATE statement on a different table using this column.
How can I achieve this in the most efficient way?
What you want to do sounds like an update with a join. Something like this:
update table2 t2 join
table1 t1
on t2.col = t1.col and t1.val = #val
set t2.col2 = 1;

Update statement in Mysql and Oracle

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);

Insert missing records from one table to another using mysql

I don't know why I am confused with this query.
I have two table: Table A with 900 records and Table B with 800 records. Both table need to contain the same data but there is some mismatch.
I need to write a mysql query to insert missing 100 records from Table A to Table B.
In the end, both Table A and Table B should be identical.
I do not want to truncate all the entries first and then do a insert from another table. So please any help is appreciated.
Thank you.
It is also possible to use LEFT OUTER JOIN for that. This will avoid subquery overhead (when system might execute subquery one time for each record of outer query) like in John Woo's answer, and will avoid doing unnecessary work overwriting already existing 800 records like in user2340435's one:
INSERT INTO b
SELECT a.* FROM a
LEFT OUTER JOIN b ON b.id = a.id
WHERE b.id IS NULL;
This will first select all rows from A and B tables including all columns from both tables, but for rows which exist in A and don't exist in B all columns for B table will be NULL.
Then it filter only such latter rows (WHERE b.id IS NULL),
and at last it inserts all these rows into B table.
I think you can use IN for this. (this is a simpliplification of your query)
INSERT INTO table2 (id, name)
SELECT id, name
FROM table1
WHERE (id,name) NOT IN
(SELECT id, name
FROM table2);
SQLFiddle Demo
AS you can see on the demonstration, table2 has only 1 records but after executing the query, 2 records were inserted on table2.
If it's mysql and the tables are identical, then this should work:
REPLACE INTO table1 SELECT * FROM table2;
This will insert the missing records into Table1
INSERT INTO Table2
(Col1, Col2....)
(
SELECT Col1, Col2,... FROM Table1
EXCEPT
SELECT Col1, Col2,... FROM Table2
)
You can then run an update query to match the records that differ.
UPDATE Table2
SET
Col1= T1.Col1,
Col2= T1.Col2,
FROM
Table T1
INNER JOIN
Table2 T2
ON
T1.Col1 = T2.Col1
Code also works when a group by and having clauses are used. Tested SQL 2012 (11.0.5058) Tab1 is source with new records, Tab 2 is the destination to be updated. Tab 2 also has an Identity column. (Yes folks, real world is not as neat and clean as the lab assignments)
INSERT INTO Tab2
SELECT a.T1,a.T2,a.T3,a.T4,a.Val1,a.Val2,a.Val3,a.Val4,-9,-9,-9,-9,MIN(hits) MinHit,MAX(hits) MaxHit,SUM(count) SumCnt, count(distinct(week)) WkCnt
FROM Tab1 a
LEFT OUTER JOIN Tab2 b ON b.t1 = a.t1 and b.t2 = a.t2 and b.t3 = a.t3 and b.t4 = a.t4 and b.val1 = a.val1 and b.val2 = a.val2 and b.val3 = a.val3 and b.val4 = a.val4
WHERE b.t1 IS NULL or b.Val1 is NULL
group by a.T1,a.T2,a.T3,a.T4,a.Val1,a.Val2,a.Val3,a.Val4 having MAX(returns)<4 and COUNT(distinct(week))>2 ;

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.