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

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

Related

I'm trying to match different tables without a primary key

before posting this, I searched for an answer, but couldn't find the exact solution to my issue.
First of all, I am only assuming that I need INNER JOIN to solve it. FULL OUTER JOIN might also do the trick.
I have 3 different tables in one database.
TABLE 1
userId
roleID
1
1
2
2
TABLE 2
userId
userName
1
A
2
B
TABLE 3
roleId
roleName
1
X
2
Y
My goal is to write a query, which matches the userName from Table 2 with the roleName of Table 3 in the output. It should look like this:
Output
UserName
RoleName
A
X
B
Y
I can't figure it out. Any help would be appreciated.
Thank you very much!
EDIT:
so far I am trying to modify the following query to do the job:
SELECT * FROM
table1.roleId
INNER JOIN
table2.roleId
ON table1.roleId = table3.roleId
INNER JOIN
table2.userId
ON table2.userId = table3.roleId
Just join all tables together with following conditions:
Table 2 userId = Table 1 userId
Table 3 roleId = Table 1 roleId
Then select only those values which are relevant to you.
I think this should do the job:
select t2.userName, t3.roleName
from table2 t2
join table1 t1 on t2.userId = t1.userId
join table3 t3 on t3.roleId = t1.roleId;
The query above uses INNER JOINs to get the result.
If there are userIds without a roleId in Table 1, FULL OUTER JOIN would also return userNames with empty roleNames if such entries are present.

How to differentiate name of columns in different tables?

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`

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();

How do I write this SQL Query to join these three tables?

I have three tables that I need to query in order to get my results.
Table 1 has an AppId and a ProjectID
Table2 has an AppID and an AppName.
Table 3 has a ProjectID and a ProjectName
I want to get out of this a list, By AppName, the ProjectNames they are tied to.
So far, a basic query to get what I want works, but I only get the ID's. I need to somehow join these to get the names associated. I need to somehow join this to table2 with the project name information, and table 2 with the appname information.
Select * from Table1 ( this table has only ID's, not names)
Order by AppId
You can join the tables like this:
Select t2.AppName, t3.ProjectName
from table1 t1
inner join table2 t2 on t2.AppID = t1.AppID
inner join table3 t3 on t3.ProjectID = t1.ProjectID

Update table based on ID correspondence

I need to copy data from one table (A) to another (B) based on id correspondence, but the correspondence of ID is actually stored into a third table (C).
So the table with the correspondence looks like
C.A_ID C.B_ID
1 33
2 56
3 74
I tried something like
UPDATE DB.A
SET DB.A = DB.B
FROM DB.A p
INNER JOIN
DB.B p1
INNER JOIN
DB.C p2
how to insert the ID correspondence?
UPDATE A , (select c.id1, b.data from B join C on (B.id2 = C.id2 )) as Foo
SET A.data = Foo.data
WHERE
A.id1 = Foo.id1
you need to use select into statement for selecting data from one table and insert in another table.