DominicEU
DominicEU

Reputation: 3631

Sub Select Inside UPDATE

I have two tables, one called 'guilds', and one called *'guild_colours'*. The fields within *guild_colours* are id, and colour (I.E: 1:FFFFFF, 2:000000..), the table guilds has to contain the hex value for the colour. As the available colours inside of *guilds_colours* are ever changing (however previously available colours are acceptable).

I need to run an UPDATE query when a user decides to change the colour of their guild. I started coming up with something like this

    UPDATE guilds g
    SET g.primarycolour = (SELECT colour FROM  guild_colours WHERE id = ? ),                                 
    g.secondarycolour = (SELECT colour FROM guild_colours WHERE id = ?)
    WHERE g.id = ?

I was wondering if there's anything faster that I could do?

Upvotes: 1

Views: 5804

Answers (1)

Chibuzo
Chibuzo

Reputation: 6117

Try this

UPDATE guilds AS g 
SET g.primarycolour = (
    SELECT c.colour FROM table_name AS c 
    WHERE c.id = 1
), g.secondarycolour = (
    SELECT c.colour FROM table_name AS c
    WHERE c.id = 2
)
WHERE g.id = 1

Note: I didn't test it though.

Upvotes: 4

Related Questions