MySQL - Update values based on subquery - mysql

let's say I have select, which return me from table1:
ID Name
1 Bob
2 Alice
3 Joe
Then I want UPDATE values in another table based on this result:
UPDATE table2 SET Name = table1.Name WHERE ID = table1.ID
As I understood, I can only do internal select in one place, like:
UPDATE table2 SET Name = (select Name from table1) WHERE ...
And I don't know how to specify WHERE-condition.

all you should do is just join the tables like this.
UPDATE table2 t2
JOIN table1 t1 ON t1.id = t2.id
SET t2.name = t1.name;
RESULTS WITH JOIN
if you are set on doing it with a select you could do it like this.
UPDATE table2 t2,
( SELECT Name, id
FROM table1
) t1
SET t2.name = t1.name
WHERE t1.id = t2.id
RESULTS FROM SELECT

UPDATE table2
SET name = (SELECT table1.Name FROM table1 WHERE table1.id = table2.id)
WHERE apply_condition
EDIT:#1
UPDATE table2 t2, (SELECT id, name FROM table1) t1 SET t2.name = t1.name WHERE t1.id = t2.id
please read this link,another

Try this
Update table2
Set Name = (Select Name From table1 where table1.ID = table2.ID)
Where table2.ID In (Select ID From table1)

Related

How to select the id with max value that in another table

Here are 2 tables.
Table 1
id value
1 3
2 2
3 3
4 1
5 4
6 3
Table 2
id
1
3
4
How do I get the ids that are in Table 2 which have the max value in Table 1?
Output:
id
1
3
I already tried the following to get the max value, but I cannot figure out how to use it in a single query to get the matching rows. Because I think I need to select from the same table I just inner joined.
select max(table1.value)
from table2
inner join table1 on table1.id = table2.id;
Here is one method:
select t2.id
from (select t2.*, rank() over (order by value desc) as seqnum
from table2 t2 join
table1 t1
on t2.id = t1.id
) t
where seqnum = 1;
Or, an alternative that puts all the ids on one row:
select group_concat(t2.id) as ids
from table2 t2 join
table1 t1
on t2.id = t1.id
group by t1.value
order by t1.value desc
limit 1;
You have a couple of options available without using window functions:
You can use a WHERE clause to select only id values that have a value equal to the MAX(value) from your query and an id that is in Table2:
SELECT t1.id
FROM Table1 t1
WHERE value = (
SELECT MAX(t1.value)
FROM Table2 t2
JOIN Table1 t1 ON t1.id = t2.id
)
AND id IN (SELECT id FROM Table2)
You can JOIN your query to Table1 and Table2 again, matching the value in Table1 and the id in Table2:
SELECT t1.id
FROM (
SELECT MAX(t1.value) AS max_value
FROM Table2 t2
JOIN Table1 t1 ON t1.id = t2.id
) t
JOIN Table1 t1 ON t1.value = t.max_value
JOIN Table2 t2 ON t2.id = t1.id
In both cases the output is
id
1
3
Demo on SQLFiddle
Too low to comment but from the SQL statement you gave, you just need to add the tableid in your select parameters.
select table2.id, max(table1.value)
from table2
inner join table1 on table1.id = table2.id;

How to insert data from table1 to table2 if they got something in common?

I got 2 tables in same database in MYSQL and I want insert the columnA in table1 to columnA in table2 and my condition is if table1.name is equal to table2.name. I tried this but didnt work
INSERT INTO Table2 (solId, openTime, closingTime, guid)
SELECT solId, openTime, closingTime, guid
FROM Table1
WHERE Table2.name = Table1.name;
You may want an update:
update table2 t2 join
table1 t1
on t1.name = t2.name
set t2.solId = t1.solId,
t2.openTime = t1.openTime,
t2.closingTime = t1.closingTime,
t2.guid = t1.guid;

SQL database : #1242 - Subquery returns more than 1 row

UPDATE table1 AS t1
INNER JOIN table2 AS t2 ON t1.table1_id=t2.table2_id
SET t1.overview=t2.val
WHERE t1.table1_id=(SELECT table2_id
FROM table2
WHERE table2_id=1);
table2 has multiple id values which are 1, so it gives
#1242 - Subquery returns more than 1 row
You need a DISTINCT.
UPDATE table1 AS t1
INNER JOIN table2 AS t2
ON t1.table1_id = t2.table2_id
SET t1.overview = t2.val
WHERE t1.table1_id = (SELECT DISTINCT table2_id FROM table2 WHERE table2_id = 1);
And if table2_id is fixed, why don't you just use 1 like:
UPDATE table1 AS t1
INNER JOIN table2 AS t2
ON t1.table1_id = t2.table2_id
SET t1.overview = t2.val
WHERE t1.table1_id = 1;
Obviously you have more than 1 record in table2 with same ID.
But if it's OK, change your = operator to in operator.
So This will be your code:
UPDATE table1 AS t1 INNER JOIN table2 AS t2 ON t1.table1_id=t2.table2_id SET t1.overview=t2.val where t1.table1_id in ( SELECT table2_id
FROM table2 WHERE table2_id=1);
Edit:
No need to subquery, it's redundant. Check this out:
UPDATE table1 AS t1 INNER JOIN table2 AS t2 ON t1.table1_id=t2.table2_id SET t1.overview=t2.val where table2_id=1;
If you indeed want to compare t1.table1_id against multiple values, use:
in instead of =
UPDATE table1 AS t1
INNER JOIN table2 AS t2 ON t1.table1_id=t2.table2_id
SET t1.overview=t2.val WHERE t1.table1_id
IN ( SELECT xxx FROM table2 WHERE table2_id=1);
BTW if you are only returning table2_id from the inner query, you can skip the inner query altogether.

Update table joined to another table with different conditions

I want to update one table which is inner join to another table, but the rows which should be updated should have specific condition or not exist in the second table:
Update T1
Set STATUS = 'R'
From table1 T1
inner join table2 T2
on T1.ID = T2.ID and T2.STATUS = 'F'
Update T1
Set STATUS = 'R'
From table1 T1
Where T1.ID not exists in(Select T2.ID from table2 T2)
How can I write it by one Query?
You can do an OR in the where clause. Like the following :
Update T1 Set STATUS = 'R' From table1 T1
inner join table2 T2 on T1.ID = T2.ID and T2.STATUS = 'F' or T1.ID not exists in(Select T2.ID from table2 T2)

MySQL Join and inner selects

I currently have a query
SELECT id FROM table1 WHERE {filters on table1} AND id NOT IN (SELECT table1ID FROM table2 WHERE condition = 0)
Table1 has a 1 - Many relationship with table2 and I'm looking for all the IDs that have no entries in table2 with condition=0.
Is there any way to rewrite this query without the inner select? I'm been scratching my head about it for a while now and any pointers would be welcome.
You can try something like
SELECT id
FROM table1 t1 LEFT JOIN
table2 t2 ON t1.ID = t2.table1ID
AND t2.Condition = 0
WHERE {filters on table1}
AND t2.table1ID IS NULL
Or just as good would be
SELECT id
FROM table1 t1
WHERE {filters on table1}
AND NOT EXISTS (
SELECT 1
FROM table2 t2
WHERE t1.ID = t2.table1ID
ADN t2.condition = 0
)