我有两个表,我想从中创建一个选择,这样第一个表中的引用 id 被第二个表中的值替换。首选针对大型数据库的优化查询。以下说明了我的目标:
TABLE_KINGDOMS
==============
kid kingdom id_king id_queen id_prince id_princess
-- ------- ------- -------- --------- -----------
1 red 1 2 3 4
2 blue 5 6 7 8
...
TABLE_PLAYERS
=============
pid username points gold
-- -------- ------ ----
1 Jack 34789 35667
2 Jill 2312 23887
3 Walt 8756 23112
4 Winnie 587 255
5 Eric 76521 34678
6 Alice 6799 7549
7 Ned 5565 9009
8 Rose 2312 3429
...
DESIRED_SELECTION (Replace id with username)
=================
kid kingdom king queen prince princess
-- ------- ---- ----- ------ --------
1 red Jack Jill Walt Winnie
2 blue Eric Alice Ned Rose
...
我尝试了以下方法,但它不起作用:
SELECT *
FROM table_kingdoms
LEFT JOIN table_users ON id_king = tu.id
LEFT JOIN table_users ON id_queen = tu.id
LEFT JOIN table_users ON id_prince = tu.id
LEFT JOIN table_users ON id_princess = tu.id
谢谢!