Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
849 views
in Technique[技术] by (71.8m points)

sql - MySQL update with select from another table

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...