Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL update with select from another table

Tags:

sql

mysql

I've got 3 tables. Companies, Kommuner and Fylker.

The companies table have an empty field forretningsadresse_fylke but an other field forretningsadresse_kommune with a value.

So basically, I need to fill in forretningsadresse_fylke, based on the value of forretningsadresse_kommune.

Now, the value of forretningsadresse_kommune and the value I want for forretningsadresse_fylke is stored in the Kommuner and Fylker tables.

So I wrote this query, but that doesn't seem to work because after 600 seconds the "MySQL server goes away".

UPDATE companies, fylker, kommuner
SET companies.forretningsadresse_fylke = (
    SELECT fylkeNavn 
    FROM fylker 
    WHERE fylker.fylkeID = kommuner.fylkeID
)
WHERE companies.forretningsadresse_kommune = kommuner.kommuneNavn

Here is what the Kommuner and Fylker tables look like.

Kommuner Table

enter image description here

Fylker Table

enter image description here

Companies table enter image description here

companies Table

            | forretningsadresse_fylke  | forretningsadresse_kommune |
            |===========================|============================|
            |                           |                            |
            |                           |                            |
            |                           |                            |
            |                           |                            |
            |                           |                            |
            |                           |                            |

So I was wondering if there was something wrong with the query? Also, it might be good to mention, the table I try to update (Companies) has over 1 million rows.

Thanks in advance!

like image 220
Kaizokupuffball Avatar asked Dec 14 '22 06:12

Kaizokupuffball


2 Answers

You do not want fylker in the UPDATE statement. You should also be using a proper join. So the first rewrite is:

UPDATE companies c JOIN
       kommuner k
       ON c.forretningsadresse_kommune = k.kommuneNavn
    SET c.forretningsadresse_fylke = (SELECT f.fylkeNavn 
                                      FROM fylker f
                                      WHERE f.fylkeID = k.fylkeID
                                     );

If we assume a single match in fylker, then this is fine. If there are multiple matches, then you need to choose one. A simple method is:

UPDATE companies c JOIN
       kommuner k
       ON c.forretningsadresse_kommune = k.kommuneNavn
    SET c.forretningsadresse_fylke = (SELECT f.fylkeNavn 
                                      FROM fylker f
                                      WHERE f.fylkeID = k.fylkeID
                                      LIMIT 1
                                     );

Note: This will update all companies that have a matching "kommuner". If there is no matching "fylker" the value will be set to NULL. I believe this is the intent of your question.

Also, table aliases make the query easier to write and to read.

like image 137
Gordon Linoff Avatar answered Dec 27 '22 13:12

Gordon Linoff


you can refer this question

https://stackoverflow.com/questions/15209414/how-to-use-join-in-update-query

    UPDATE companies c
    JOIN Kommuner k ON c.kommuneID = k.kommuneID
    JOIN fylker f ON f.fylkeID = k.fylkeID
    SET c.forretningsadresse_fylke = f.fylkeNavn
like image 22
keronconk Avatar answered Dec 27 '22 12:12

keronconk