MYSQL Join two tables with one column

I have two tables: table 1 = university and table 2 = school

I added university_idto table 2 and I need to join two tables.

If university_namefrom table 1 and namefrom table 2 are identical, get idfrom table 1 and replace it with table 2university_id

I am very new to sql, so if you could explain that would be great. I also tried the following to no avail!

     select a.id,b.name from university as a
     inner join school as b on a.university_name = b.name
     UPDATE `school` SET `university_id` = a.id WHERE a.university_name = b.name
+4
source share
2 answers

Sort of

UPDATE school a 
JOIN university b ON a.university_name = b.name
SET a.university_id = b.id

must work

+1
source

I can't run the test right now ... Maybe this gives you a hint.

UPDATE `school` s SET `university_id` = (SELECT u.id FROM `university` u WHERE u.name=s.university_name)

You may need to JOIN the school desk in the SELECT statement.

+1

Source: https://habr.com/ru/post/1628587/


All Articles