开发者

mysql, how to join this key id?

开发者 https://www.devze.com 2023-03-17 21:48 出处:网络
Forgive me, I am new to mysql I have this mysql table: 开发者_StackOverflowID|KeyID1|KeyID2|Param|Value|...

Forgive me, I am new to mysql

I have this mysql table:

|开发者_StackOverflowID|KeyID1|KeyID2|Param|Value|...
|asdf|1|2| ...

The KeyID1 and KeyID2 values exist another Table like this:

|KeyID|Real Name|
| 1   | Gordon 
| 2   | Jason 

I want to be able to join the two tables together such that I would have ID|Real Name1|Real Name2|Param|Value

what would i need to do in mysql? Would I need to duplicate another table?

edit: i can change the database design


This is a classic JOIN operation:

SELECT ID, tab1.RealName as RealName1, tab2.RealName as RealName2, Param, Value
       FROM mytable JOIN nametable AS tab1 ON (mytable.KeyID1 = tab1.KeyID)
                    JOIN nametable AS tab2 ON (mytable.KeyID2 = tab2.KeyID) ;

I'm assuming that your first table is called mytable and that the name lookup table is called nametable, and I also removed the spaces from column names.

Whenever you need to replace a foreign unique identifier by values from the corresponding lookup table, it's time for a JOIN on that lookup table. If you do it multiple times from the same table, you can use aliases (AS) to disambiguate which instance you are referring to.

0

精彩评论

暂无评论...
验证码 换一张
取 消