Update from select query in mysql - mysql

I have used the following query to insert values in table
INSERT INTO `tbl1` SELECT * FROM tbl2
Over here tb1 is a temp table.
Now, I want something like this
UPDATE `my_table` SELECT * FROM tbl1
I know that the syntax for update is Update tbl SET cols = vals
But can we have something like the insert query above ?
Thanks.

You can doInsert with Select but not Update with Select. But still possible by using JOIN within UPDATE.
UPDATE table1 t1 JOIN table2 t2 ON t1.id = t2.id
SET t1.col1 = t2.col2, t1.col2 = t2.col2

You can join your tbl1 table with my_table using the multiple-table UPDATE syntax:
UPDATE my_table JOIN tbl1 ON ***join_condition***
SET my_table.foo = tbl1.bar, ...

You can do something like this:
update my_table join tbl1 on my_table.id = tbl1.id
set my_table.Vaal= tbl1.vaal

Related

Update query with multiple tables and limit option

Update table2 t2
set t2.c1=t1.c1
inner join table1 t1 on ( t1.c2 = t2.c2)
where t2.c1 = "-1";
I want to execute the above query which will update the table2 column from table1 column ON INNER JOIN matching conditions. It is working fine. I am running a migration where the rows count are in million in both tables. I thought of limiting the update query in batches for query optimization but limit is not allowed in update query.
I can try with select query with limit option, but updating multiple columns would not work with this below query.
update table2 t2
set t2.c1=<?>
where t1.c2 = ( select c2 from table);
Can anyone help to use update query with optimization? Will updating millions row have any impact?
You could move the limiting clause to the joined table, like so:
update table2 t2
inner join (
select c1, c2
from table1
order by c2
limit ?, ?
) t1 on t1.c2 = t2.c2
set t2.c1 = t1.c1
where t2.c1 = -1
I am not sure what you really want to do, but you can update multiple columns with update and limit.
The following is fine:
update table2 t2
set t2.c1 = <?>,
t2.c3 = ?
where t1.c2 = ( select c2 from table)
limit 100;

SQL - delete record from table X if same record not in table Y

sorry to make a big fuss about delete same records again, but i can't find any solution :(
i need something like that:
if a select of 3 columns values from table1
is NOT equal to the same select in table2
than delete record from table2
that are my aproaches:
DELETE FROM table2
INNER JOIN table1
ON table2.basketid = table1.basketid AND
table2.artid = table1.artid
WHERE table1.userid='007'
DELETE FROM table2
WHERE NOT EXISTS (
SELECT basketid, artid
FROM table1
)
AND userid ='007'
Thanks for your help or any tipp!!
you can do this in SQL Server
DELETE t2
FROM Table2 t2
WHERE NOT EXISTS ( SELECT 1
FROM Table1 t1
WHERE t1.basketid = t2.basketid
AND t1.artid = t2.artid )
AND t2.userid = '007'
notice the filters in the select that compares the values in Table2 to Table1
if userid is also in Table2 you might want to add that to the WHERE also
DELETE t2
FROM Table2 t2
WHERE NOT EXISTS ( SELECT 1
FROM Table1 t1
WHERE t1.userid = t2.userid
AND t1.basketid = t2.basketid
AND t1.artid = t2.artid )
AND t2.userid = '007'
Could be you need a where ( ) not in ( )
DELETE FROM table2
WHERE (basketid, artid ) NOT IN (
SELECT basketid, artid
FROM table1
)
AND userid ='007';
DELETE y
FROM X
LEFT JOIN Y
ON
X.COL1=Y.COL1
AND
X.COL2=Y.COL2
AND
X.COL3=Y.COL3
WHERE
Y.COL1 IS NULL
Try the above code.

SELECT fields where there no exist others in another table

I have the following table schema:
Table 1
-
field1
Table 2
-
field1 | field2
What I want to do is select field2 from the second table where field1 in the second table doesn't exist in the first table (field1).
I had this :
SELECT t2.field2
, t2.field1
FROM table1 t1
, table2 t2
WHERE t2.field1 != t1.field1
The problem is that this query will retrieve multiple repeated information from table2 if multiple rows apply in table1. I added DISTINCT and/or LIMIT but it still doesn't work. Any idea on how to do this?
You can use a subquery:
SELECT t2.field2, t2.field1 FROM table2 t2 WHERE t2.field1 NOT IN (SELECT t1.field1 FROM table1 t1);
This will give you all rows from table2 that have a field1 which is not in table1.
You can now use DISTINCT or LIMIT on the outermost query for any further processing.
You can use LEFT JOIN together with IS NULL check :
SELECT DISTINCT t2.field2
FROM table2 t2
LEFT JOIN table1 t1 ON t1.field1 = t2.field1
WHERE t1.field1 IS NULL
The title of your question is almost the command you need: 'NOT EXIST'. SQL is so simple. ;)
SELECT DISTINCT t2.field2
FROM Table2 t2
WHERE NOT EXISTS (SELECT * FROM Table1 WHERE t1.field1 = t2.field1)

Inner join and delete not working in mysql

I have two tables table1 and table2. I want to delete from table1 based on a condition in table2.
I have the following mysql query:
DELETE FROM table1
INNER JOIN table2 ON table2.col1 = table1.col1
WHERE table2.col2 = '1'
This return a syntax error. Is there something wrong with the above syntax?
You need to specify the table you are deleting from:
DELETE table1
FROM table1 INNER JOIN
table2
USING (col1)
WHERE table2.col2 = '1';
Try this:
DELETE FROM table1
WHERE EXISTS(
SELECT 'C'
FROM table2
WHERE table2.col1 = table1.col1
AND table2.col2 = '1'
)
You could do something like:
DELETE FROM table1 WHERE col1 IN (select col1 from table2 WHERE table2.col2 = '1');

MySQL UPDATE syntax with multiple tables using WHERE clause

Case:
How to update table1 with data from table2 where id is equal?
Problem:
When I run the following update statement, it updates all the records in table1 (even where the id field in table1 does not exist in table2).
How can I use the the multiple update table syntax, to update ONLY the records in table1 ONLY where the id is present in table2 and equal?
UPDATE table1,table2
SET table1.value=table2.value
WHERE table2.id=table1.id
Thanks in advance.
here's the correct syntax of UPDATE with join in MySQL
UPDATE table1 a
INNER JOIN table2 b
ON a.ID = b.ID
SET a.value = b.value
SQLFiddle Demo
EDIT
For MySql it'll be
UPDATE table1 t1 INNER JOIN
table2 t2 ON t2.id = t1.id
SET t1.value = t2.value
sqlfiddle
Original answer was for SQL Server
UPDATE table1
SET table1.value = table2.value
FROM table1 INNER JOIN
table2 ON table2.id=table1.id
sqlfiddle
You can try this:
UPDATE TABLE1
SET column_name = TABLE2.column_name
FROM TABLE1, TABLE2
WHERE TABLE1.id = TABLE2.id
UPDATE table1
SET table1.value = (select table2.value
WHERE table2.id=table1.id)