How to differentiate name of columns in different tables? - mysql

I have two tables, their structure looks like this:
table 1: id, name, description
table 2: id, otherDescription
I want to set the value of "description" in table #1 to value "otherDescription" from table #2 respectively this id. I wrote the query:
UPDATE `table_1` SET description = (SELECT oldDescription FROM table_2 WHERE id = id))
How MySql will knows that in expression WHERE id = id first id taken from one table, and other id taken from second table? How to write this query correct? I think here must be used AS, but i dont know where

you can use table mane eg :
UPDATE table_1
INNER JOIN table_2 ON table_1.id = table_2.id
SET table_1.description = table_2.oldDescription
or table name alias
UPDATE table_1 a
INNER JOIN table_2 b ON a.id = b.id
SET a.description = b.oldDescription

You can join the two tables in the UPDATE, and give each an alias. The you identify the columns using alias.columnname:
UPDATE `table1` a
JOIN `table1` b ON a.`id` = b.`id`
SET a.description` = b.`otherDescription`

Related

complex sql query to update a table if an id is in

i would like to update massively a table regarding an other one.
I have an commun id and i would like to update a row if the id is in the second table.
Table 1 :
ID_table1|is_in_db2
Table 2 :
ID_table2|ID_table1
I want to update is_in_db2 to 1 if the ID_table1 is in table 2
You want to join the tables using an inner join on the ID.
This will update the specified column for any row that appears in both tables.
UPDATE
table1
INNER JOIN table2 ON table1.ID = table2.ID_table1
SET
table1.is_in_db2 = 1;

SQL How to connect two tables together by a specific column?

I have two tables: university and university_list
Table1 - university
Table2 - university_list
I added university_id into the table 2 and I need to connect the two tables.
If university_name from table 1 and name from table 2 are identical, get the id from table 1 and replace it onto table 2 university_id
Thank you in advance!
select a.id,b.name from table1 as a
inner join table2 as b
on a.university_name = b.name
Above query will return id and name of university if match. Hold both value in variable and pass variable in update query.
update table2 set university_id = '$val' where b.name = '$name';
It is a simple join update
You can update table 2 using below query
update ul
set university_id = u.id
from
university u inner join university_list ul on ul.name = u.university_name
you also can refer to Join Update
UPDATE university_list a
JOIN university b ON a.name = b.university_name
SET a.university_id = b.id

Oracle Select Query based on Column value

I have two tables lets say
Table A
columns id , name address
Table B
columns id , age, import_date
The Table B id is a reference key of Table A.
Now I want to return results from A & B but if the record is not in B I still want to see the record so for this I use left outer join
Select * from A a left join B b
on a.id = b.id
Now even I don't have record in B I still get the record.
Table B may contain duplicate ids but unique import_date.
Now I want to results in a way that if there is duplicate id in table B then I want to get the records only where import_date is as of today.
I still want to get the records for ids which are not there but if the ID is there in table B then I want to apply above condition.
I hope someone can help me with this.
Sample data
Table A
01|John|London
02|Matt|Glasgow
03|Rodger|Paris
Table B
02|22|31-AUG-2015
02|21|30-AUG-2015
02|23|29-AUG-2015
The query will return
01|John|London|null|null|null
02|Matt|Glasgow|22|31-Aug-2015
03|Rodger|Paris|null|null
You almost got the solution. Just add one more condition like below
Select a.id,a.name,a.address,b.age,b.import_date
from tablea a left join tableb b
on a.id=b.id and b.import_date=trunc(sysdate)
order by a.id;---This line optional
Check the DEMO HERE
SELECT *
FROM Table_A t1 LEFT OUTER JOIN Table_B t2 ON t1.id=t2.id UNION
SELECT *
FROM Table_A t1 LEFT OUTER JOIN Table_B t2 ON t1.id=t2.id
GROUP BY t2.import_date
HAVING t2.import_date=CURDATE();

update one column with data from other column different table

I want to update a column from one table to the column in another table. Both columns have the same name and same ID and are populated. I need to update only IDs which match in both tables ( column id)
So for example all values from table_1 in column_x to be copied to table_2 in column_x if both are with same column_id
Fixed with help from other site.
tried: UPDATE table1 JOIN table2 ON column1.column_x = table2.column_x SET table2.id = table1.id;
It looks like you've mixed up which column to join on and which column to copy. Your stated problem is:
all values from table_1 in column_x to be copied to table_2 in column_x if both are with same column_id
The query to do this would be the following:
UPDATE table1 JOIN table2 ON table1.column_id = table2.column_id
SET table2.column_x = table1.column_x;
Or slightly more concisely:
UPDATE table1 JOIN table2 USING (column_id)
SET table2.column_x = table1.column_x;

Is it possible that update a table with results with another nested query which inlude the queried table itself?

I have two tables. First has id (AI) field and old_id (Int) field.
Second table has a referred field p_id from first tables old_id as a relation point.
I want to update p_id to firstTable.id values. Is it possible to update secondTable.p_id with the value returned from firstTable.id ?
Here is my test sql:
UPDATE secondTable sT
SET sT.p_id = (
SELECT fT.id
FROM firstTable fT
WHERE fT.old_id = secondTable.p_id
)
You can update with JOIN like this:
UPDATE secondTable sT
INNER JOIN firstTable fT ON fT.old_id=secondTable.p_id
SET sT.p_id = fT.id;