Update a table joining 1 more table.
UPDATE t1 SET t1.col1 =1 FROM table1 t1 JOIN table2 t2
ON t1.ID=t2.ID
WHERE t1.Name='Test' AND t2.Age=25;
i get this error,You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM table1 t1 JOIN table2 t2 ...
Any thoughts?
Thanks.
There shouldn't be a FROM clause in the UPDATE statement, and the SET clause should follow the full set of table references:
UPDATE table1 t1
JOIN table2 t2 ON t1.ID = t2.ID
SET t1.col1 = 1
WHERE t1.Name = 'Test' AND t2.Age = 25;
Test case:
CREATE TABLE table1 (id int, col1 int, name varchar(20));
CREATE TABLE table2 (id int, age int);
INSERT INTO table1 VALUES (1, 0, 'Test');
INSERT INTO table1 VALUES (2, 0, 'Test');
INSERT INTO table1 VALUES (3, 0, 'No Test');
INSERT INTO table2 VALUES (1, 20);
INSERT INTO table2 VALUES (2, 25);
INSERT INTO table2 VALUES (3, 25);
Result:
SELECT * FROM table1;
+------+------+---------+
| id | col1 | name |
+------+------+---------+
| 1 | 0 | Test |
| 2 | 1 | Test |
| 3 | 0 | No Test |
+------+------+---------+
3 rows in set (0.00 sec)
UPDATE table1 SET col1 = 1
from table1 t1
JOIN table2 t2 ON t1.ID = t2.ID
WHERE t1.Name = 'Test' AND t2.Age = 25;
I have a problem in update join. in table1 i saved username instead of userid.
And for username datatype was varchar(10). When name exceeds 10 then name is saved with 10 character only. Now when i try to update using join query it doesn't works on those fields that have not exact unername in users table,
Notice name bill gat in table1 but the name in users field was bill gates
-es missing.
SELECT * FROM table1;
+------+------+---------+
| id | col1 | created_by|
+------+------+---------+
| 1 | 0 | steve jobs|
| 2 | 1 | bill gat |
| 3 | 0 | Jones |
+------+------+---------+
3 rows in set (0.00 sec)
===I solved this way
UPDATE table1 AS tr
JOIN users AS u ON u.name LIKE CONCAT('%', tr.created_by, '%')
SET tr.created_by=u.id
WHERE tr.created_by IS NOT NULL
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
| 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)
I have the following problem. I have 2 tables in the database - table1 and table2.
Table1
id| val1| val2
--------------
1 | 234 | 342
2 | 325 | 356
...
Table2
id | uid | val
--------------
1 | 5 | 234
2 | 6 | 362
3 | 5 | 123
I would like to check for each record in table2 if val exists in table1 (table2.val=table1.va1 or table2.val > table1.vall).
In table1 is about 2 million records. In table2 several thousand.
If query result true i'd like to remove rows from table2.
Is it possible to do this in one query? mysql or postgresql
Performance is very important.
Assuming (t2.val = t1.val1) or (t1.val2 > t2.val) conditions:
delete table2
from table2 t2
inner join table1 t1 on (t2.val = t1.val1) or (t1.val2 > t2.val)
delete t
from #table2 t
inner join #table1 t2
on t.val = t2.val or t2.val>t.val
Assuming uid in Table2 reference id in Table1 (and that neither table contain nulls), this returns the rows in Table1 that satisfy your existence criteria:
SELECT *
FROM Table1
WHERE EXISTS (
SELECT *
FROM Table2
WHERE Table2.uid = Table1.id
AND (
Table2.val = Table1.val1
OR Table2.val > Table1.val2
)
);
I have a table with an id column that references a row from the same table that represents a set of default values the row should return if any of the values are NULL. For example:
'content'
id | a | b | default_id
----------------------------------
1 | 33 | 55 | NULL
2 | NULL| 11 | 1
So I want to query row 2 in a way that I get back a result with 'a' = 33 and 'b' = 11. Is there some simple way to do this?
You may want to use the COALESCE() function, whick returns the first non-NULL value in the list:
SELECT COALESCE(c1.a, c2.a) a ,
COALESCE(c1.b, c2.b) b
FROM content c1
LEFT JOIN content c2 ON (c2.id = c1.default_id)
WHERE c1.id = 2;
Test case:
CREATE TABLE content (id int, a int, b int, default_id int);
INSERT INTO content VALUES (1, 33, 55, NULL);
INSERT INTO content VALUES (2, NULL, 11, 1);
Result:
+------+------+
| a | b |
+------+------+
| 33 | 11 |
+------+------+
1 row in set (0.00 sec)
select t1.id
,coalesce(t1.a, t2.a) as a
,coalesce(t1.b, t2.b) as b
from content t1
left outer
join content t2 on(t1.default_id = t2.id)
where t1.id = 2;
SELECT t1.id, IFNULL(t1.a, t2.a), IFNULL(t1.b,t2.b)
FROM content t1
LEFT JOIN content t2 ON (t2.id = t1.default_id)
WHERE t1.id = 2