I have two tables:
TABLE1
| ColumnA | ColumnB |
-----------------------
| 123 | 1 |
TABLE2
| ColumnA | ColumnC |
-----------------------
| 123 | 20 |
I altered Table 2 to add a new column, so it looks like this
NEW TABLE2
| ColumnA | ColumnC | ColumnB |
----------------------------------
| 123 | 20 | NULL |
Now, I want to select the values of TABLE1.Column B and insert it into TABLE2.Column B
So I wrote this query:
INSERT INTO TABLE2 (ColumnB)
SELECT t1.ColumnB
FROM TABLE1 AS t1
INNER JOIN
TABLE2 AS t2
ON t1.ColumnA = t2.ColumnA;
But it doesn't seem to do the trick. Am I missing something?
You need to use an UPDATE statement to update the table. An INSERT will add new rows.
UPDATE Table2
JOIN Table1
ON
Table2.columnA = Table1.ColumnA
SET
Table2.ColumnB = Table1.ColumnB
WHERE
Table2.ColumnB IS NULL
You don't appear to want to insert. It appears you want to update the rows already in table2. You can use:
update table2 set ColumbB=(select ColumnB from table1 where table1.ColumnA=table2.ColumnA)
Related
I have two tables with a VARCHAR column called "name1" and "name2":
table1:
id | name1
1 | xyz
2 | foo
3 | barfoo
4 | xchad
table2:
id | id_table1 | name2
1 | NULL | xchad
2 | NULL | foo
3 | NULL | hade
4 | NULL | bar
I want to update the column id_table1 of table2 with the respective id from table1 where the rows name1 and name2 match.
For example in table2 the first row should be updated with 4 in column id_table1 since 'xchad' = 'xchad'.
A join simply takes too much time with the string compare.
Thank you!
Consider:
UPDATE table1 t1
INNER JOIN table2 t2 ON t2.name2 = t1.name1
SET t2.id_table1 = t1.id
With indexes on table1(name1) and table2(name2), this should perform efficiently.
An alternative is to use a correlated subquery:
UPDATE table2 t2
SET t2.id_table1 = (
SELECT t1.name1 FROM table1 t1 WHERE t1.name1 = t2.id_table1
)
Please note that this second solution does require each name in table2 to have a unique match in table1.
I am using MySQL. Let's say I have these two tables:
table 1
+---------+
| product |
+---------+
| 1 |
| 2 |
+---------+
table2
+------+---------+
| name | product |
+------+---------+
| A | 1 |
| A | 2 |
| B | 1 |
| B | 3 |
| C | 1 |
+------+---------+
which are produced using the following code:
CREATE TABLE table1(
product INT
);
CREATE TABLE table2(
name VARCHAR(10),
product INT
);
INSERT INTO table1 VALUES(1);
INSERT INTO table1 VALUES(2);
INSERT INTO table2 VALUES('A', 1);
INSERT INTO table2 VALUES('A', 2);
INSERT INTO table2 VALUES('B', 1);
INSERT INTO table2 VALUES('B', 3);
INSERT INTO table2 VALUES('C', 1);
I would like to produce a table with names from table2, for which its products match all products of table1. In this case, simply
+------+
| name |
+------+
| A |
+------+
That's the name of the retailer for which all products match the ones in the other table.
This is probably something simple that I am failing to see. I have tried inner joins, using all with a subquery, ... but...
I ended up being able to solve this using:
SELECT nome
FROM table2
WHERE product IN (SELECT product FROM table1)
GROUP BY nome HAVING COUNT(*) = (SELECT COUNT(*) FROM table1);
Based on check if a column contains ALL the values of another column - Mysql
create table3 then execute the following
INSERT INTO table3 (name)
SELECT DISTINCT t2.name
FROM table2 t2
LEFT JOIN table1 t1 on t2.product = t1.product
WHERE t1.product IS NOT NULL
You can use a join to get any matches. And then having to check they are all there.
Assuming no duplicates:
select t2.name
from table2 t2 join
table1 t1
using (product)
group by t2.name
having count(*) = (select count(*) from table1);
I have two tables table1 and table2.
I'm doing some changes and I realized that table2 is not needed, but this table has lots of data already and I need to pass the values of ID_B from table2 to table1.
Here's the structure:
table1
ID_table1 | ID_table2 | ID_B
1 | 1 |
2 | 3 |
3 | 1 |
4 | 2 |
table2
ID_table2 | ID_B
1 | 14
2 | 26
3 | 26
So what I want is the MySQL query to pass the ID_B value from table2 to table1 when the ID_table2 on table1 is equal to the ID_table2 on table2.
For example, the row on table1 where the ID_table1 is 1 would have the ID_B = 14.
Can you help me on this?
Thanks in advance,
Miguel.
Using JOINs you can do as.
update table1 t1
inner join
table2 t2 on t2.ID_table2 = t1.ID_table2
set t1.ID_B = t2.ID_B
DEMO
You could try it like so:
UPDATE
table1 AS target,
(SELECT ID_table2, ID_B FROM table2) AS source
SET
target.ID_B = source.ID_B
WHERE
target.ID_TABLE2 = source.ID_table2
I'm faced with a problem where I need to update one table based on values stored in another. However, the second table contains rows which are not relevant to the query. For example:
Table1
id | active
------------
1 | Yes
2 | Yes
3 | Yes
4 | Yes
Table2
id | type | value
--------------------
1 | date | 2011
1 | name | Glen
2 | date | 2012
2 | name | Mike
I want to read the values of type 'date' and skip name, and update table1 in the process.
I've put together the following:
UPDATE table1 a, tabel2 b
SET a.active='no'
WHERE a.id = b.id
AND b.type='date'
AND b.value='2011'
This doesn't seem to work well at all.
Any help would be great.
id is the key which joins the tables.
UPDATE table1 a, tabel2 b
SET a.active='no'
WHERE a.id = b.id
AND b.type='date'
AND b.value='2011'
Try this:
UPDATE table1
SET active = 'no'
WHERE a.id
IN (
SELECT b.id FROM table2 WHERE type = 'date' AND value = '2011'
)
This will work with a natural join
UPDATE table1
SET active='no'
WHERE id in
(
select id from table1 natural join table2
where
type='date'
AND value='2011'
)
I have 2 tables which contain many columns. example of my tables:
table1
_______________________________________________
| a | b | c | d | e | f | g | h | i | ... | z |
-----------------------------------------------
table2
_______________________________________________
| a | b | c | d | e | f | g | h | i | ... | z |
-----------------------------------------------
And now, I want to copy or insert a record from table1 to table 2. This is my query :
INSERT INTO table2
SELECT table1.* FROM table1
WHERE table1.b = '1'
I don't find any errors in query, but all I want is insert a record from all columns except column 'a' in table 1 to table2.
I can do it by this query :
INSERT INTO table2 (b,c,d,...) // it takes a long line
SELECT table1.b,table1.c,table1.d,... FROM table1 // it takes a long line
WHERE table1.b = '1'
But this is not an efficient query line, because i just don't select 1 column.
Is there any efficient way?
You could try duplicating the table with a create like statement, then alter it to drop the columns you don't want and do the insert into select with that new table. Then drop that new table. (or use a temp table)
CREATE TABLE table3 LIKE table1
ALTER TABLE table3 DROP COLUMN x
INSERT INTO table2
SELECT * FROM table3
DROP TABLE table3
and so forth.