Update MySQL table column from another table entities - mysql

I have 2 tables and I need to update a column from table2 using a column in table1. table2.id2 is empty and I must fill it using table1.id. Also, you must know that I have 2 columns that can be matched with each other in these tables (table1.code and table2.code).This is my SQL :
UPDATE table2 SET table2.id2 = table1.id WHERE table2.code = table1.code;
Is this query right ? I'm getting this error, while I'm sure that table1.code exists.
[Err] 1054 - Unknown column 'table1.code' in 'where clause'

Assuming you can join both tables using code
UPDATE T2
JOIN T1 ON T1.CODE = T2.CODE
SET
T2.ID2 = T1.ID
WHERE
T2.ID2 = '';

Doesn't work that way.
Take this:
UPDATE table2 SET id2 = (SELECT id from table1 WHERE code = 'somecode') WHERE code = 'somecode';

Related

SQL query returns "more than one row returned by a subquery used as an expression"

I have two tables:
table1 has columns name a,b and c.
table2 has columns name d and e.
I need to set table1.a with the value of table1.b only if table1.c=table2.d and table2.e='true' (it's a bool).
I wrote the following:
UPDATE table1 SET a=(
SELECT t1.b
FROM table1 t1
INNER JOIN table2 t2
ON t1.c = t2.d
WHERE t2.e = 'true');
and got of course:
ERROR: more than one row returned by a subquery used as an expression
********** Error **********
ERROR: more than one row returned by a subquery used as an expression
SQL state: 21000
How do I change this to work?
Join the tables like this:
UPDATE table1 t1
INNER JOIN table2 t2
ON t1.c = t2.d
SET t1.a = t1.b
WHERE t2.e;
If (as you say) t2.e is boolean then WHERE t2.e is enough.
If it was varchar then you would need WHERE t2.e = 'true'.
I would recommend exists:
UPDATE table1 t1
SET t1.a = t1.b
WHERE EXISTS (SELECT 1
FROM table2 t2
WHERE t1.c = t2.d AND t2.e = 'true'
);
For performance you want an index on table2(d, e). Using exists means that MySQL will not attempt to update a row twice if there are multiple matching rows in table2.

Use SELECT result to query another table's row

I have a pretty simple MySQL query to implement, but I can't figure out how...
I have two tables, T1 and T2.
What I need to do:
From T1, I retrieve an ID based on a CODE value:
SELECT id FROM T1 WHERE code = '$code';
Then I need to use this ID (so the value I just retrieved) to update a specific row in T2 (the name of the row will match the ID's value).
I was thinking about using either subqueries or user-defined variables, but no matter how I try it I can't get it done.
If you have any code snippet that can help me doing that, I would appreciate it as well!
EDIT
Just to clarify something: I don't know the name of the column that I need to update in T2, since that name will be the value I retrieve from T1.
So for example, if the ID I get from T1 is "03", it will update the column named "03" in T2.
EDIT 2
Here's a little schema of what I intend to achieve (hoping I make myself clearer, I'm sorry for the misunderstanding...)
UPDATE T2 SET COL = YOUR_VALUE
WHERE T2.ID = (SELECT id FROM T1 WHERE code = '$code')
UPDATE: If the sub query returns more than one row then you can use from IN operator
UPDATE T2 SET COL = YOUR_VALUE
WHERE T2.ID IN (SELECT id FROM T1 WHERE code = '$code')
Use an UPDATE with a JOIN:
UPDATE T2
CROSS JOIN T1
SET T2.`0` = IF(T1.id = 0, T1.someColumn, T2.`0`),
T2.`1` = IF(T1.id = 1, T1.someColumn, T2.`1`),
T2.`2` = IF(T1.id = 1, T1.someColumn, T2.`1`)
WHERE T1.code = '$code'
Replace someColumn with the column in T1 containing the value you want to put into T2.
you can update without using the subquery just using join
update t2
inner join t1 on t2.name = t1.id and t1.code ='$code'
set t2.my_col = 'my_value'
but you should not use var in your query you are at risk for sql injection take a look at you mysql driver for param_binding
UPDATE T2 SET COL = YOUR_VALUE WHERE EXISTS
(SELECT 1 FROM T1 WHERE T2.id=T2.id AND code = '$code')
You can update T2 with the next SQL if you expect at least one row from the inner query
UPDATE T2 SET COLUMN = VALUE
WHERE T2.ID IN (SELECT ID FROM T1 WHERE CODE = $code)
Otherwise, if from T1 you are sure you will get only 1 record
UPDATE T2 SET COLUMN = VALUE
WHERE T2.ID = (SELECT ID FROM T1 WHERE CODE = $code)

Column update from one Table to another based on a ID match

I have two tables in the same database with names table1 and table2. What I need is I have to copy selected columns from table1 and update those columns in table2 based on the ID match. What I have done is :
$sql = " UPDATE table2, table1 SET table2.column1= table1.column1
where table1.primarykey= table2.primarykey && primarykey= 1 ";
This query is not throwing any error but its not updating .I am a pure beginner in mysql field. Can anyone help me out ?
Your query has an error. The second primarykey reference needs a table alias; otherwise, it is ambiguous.
I would suggest writing the query using explicit join syntax:
UPDATE table2 JOIN
table1
ON table1.primarykey = table2.primarykey
SET table2.column1 = table1.column1
WHERE table1.primarykey = 1 ;
Because you did not catch this error. I would also suggest checking for errors when you run a SQL statement at the application layer.
You can try this:
sql_query = UPDATE table1 t1, table2 t2
SET t1.field1 = t2.field1, t1.field2 = t2.field2 WHERE t1.id = t2.id;
slight modification with subquery:
$sql = " UPDATE table2 SET table2.column1= ( SELECT table1.column1 from table1
where table1.primarykey= table2.primarykey && primarykey= 1 )";

Update Statement With Where Clause To Other Tables

Basically I have an update statement which needs to update two fields of a table but is dependent on its where clause which references other tables within the database.
For example.
UPDATE TABLE_ONE
SET VALUE_ONE=1,VALUE_TWO=2
WHERE TABLE_TWO.ID = 1818 AND TABLE_TWO.POSITION = TABLE_THREE.ID AND TABLE_ONE = TABLE_THREE.VALUE = TABLE_ONE.ID;
My question is how do I do this successfully. At the moment I get unknown column exception on the first parameter of the where clause.
I hope this is clear. Any help would be greatly appreciated.
It's hard to tell without seeing table schema for your all tables but you can try to rewrite your update like this
UPDATE table_one t1 JOIN table_three t3
ON t1.id = t3.value JOIN table_two t2
ON t3.id = t2.position
SET t1.value_one = 1, value_two = 2
WHERE t2.id = 1818

MySql Query If Field equals Field in different table update different field

I have got two tables. I want to update MODEL in table2 when ITEM in table1 equals ITEM in table2.
Any Ideas?
In MySQL, you do it like this
UPDATE table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id
SET t1.col1 = t2.col1,
t1.col2 = t2.col2
If I understand correctly, you just want to perform an UPDATE on table2 based on, presumably, foreign keys?
If that's right, this should work:
UPDATE
table2
JOIN table1
ON table1.ITEM = table2.ITEM
SET
MODEL = 'new value';
The table declaration in an UPDATE statement is the same as is specified in a SELECT statement - so you can use any type of JOIN that fits your table/data.
Docs for UPDATE, SELECT.
If you could add an actual query attempt, or something, that might be helpful. Can you try something like the following:
UPDATE table2 JOIN table1 ON table2.ITEM = table1.ITEM SET MODEL = ?