0

我有两个表,我想从中创建一个选择,这样第一个表中的引用 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

谢谢!

4

2 回答 2

3

只需为连接的表提供不同的别名。

SELECT k.kid, k.kingdom,
   uk.username AS king,
   uq.username AS queen,
   up.username AS prince
   ups.username AS princess
FROM table_kingdoms k
LEFT JOIN table_users uk ON k.id_king = uk.pid
LEFT JOIN table_users uq ON k.id_queen = uq.pid
LEFT JOIN table_users up ON k.id_prince = up.pid
LEFT JOIN table_users ups ON k.id_princess = ups.pid;

此外,LEFT JOIN可以省略,因为我假设id_...字段不能是NULL.

于 2012-06-19T14:11:41.953 回答
2

我用 替换了你LEFT JOINSINNER因为你在 table_kingdoms 中的 ID 都不是 null,LEFT如果 id 可以为 null,则将其放在后面,否则INNER更好。您的查询列和表名与结构中的不同,所以我不确定使用哪个。因此,如果这不能满足您的需求,请指定您所说的不起作用,我们可以解决具体问题。

SELECT K.kid,K.Kingdom, P.Username as King,PQ.Username as Queen,
PP.Username as Prince,PS.Username as Princess
FROM table_kingdoms K
INNER JOIN TABLE_PLAYERS PK ON K.id_king = PK.pid --replace TABLE_PLAYERS  with table_users depending on the actual table name
--LEFT JOIN table_users ON id_king = tu.id REPLACE WITH ABOVE
INNER JOIN TABLE_PLAYERS PQ ON K.id_queen = PQ.pid --replace TABLE_PLAYERS  with table_users depending on the actual table name
--LEFT JOIN table_users ON id_queen = tu.id  REPLACE WITH ABOVE
INNER JOIN TABLE_PLAYERS PP ON K.id_prince = PP.pid --replace TABLE_PLAYERS  with table_users depending on the actual table name
--LEFT JOIN table_users ON id_prince = tu.id  REPLACE WITH ABOVE
INNER JOIN TABLE_PLAYERS PS ON K.id_princess = PS.pid --replace TABLE_PLAYERS  with table_users depending on the actual table name
--LEFT JOIN table_users ON id_princess = tu.id  REPLACE WITH ABOVE

就您的原始查询而言,它非常接近。最重要的是您说的是 tu.id 但从未将其声明为别名并且您不能重用相同的别名然后,您必须按照您想要的方式定义列,但这不是错误。这是我上面提到的实际错误的编辑:

SELECT *
FROM table_kingdoms
LEFT JOIN table_users tu ON id_king = tu.id
LEFT JOIN table_users  tu2 ON id_queen = tu2.id
LEFT JOIN table_users tu3 ON id_prince = tu3.id
LEFT JOIN table_users  tu4 ON id_princess = tu4.id

就像一个注释,我个人也会给 table_kingdoms 起别名,这里也包括列显示:

SELECT K.kid,K.Kingdom, tu.Username as King, tu2.Username as Queen,
tu3.Username as Prince,tu4.Username as Princess
FROM table_kingdoms k
LEFT JOIN table_users tu ON k.id_king = tu.id
LEFT JOIN table_users  tu2 ON k.id_queen = tu2.id
LEFT JOIN table_users tu3 ON k.id_prince = tu3.id
LEFT JOIN table_users  tu4 ON k.id_princess = tu4.id
于 2012-06-19T14:18:15.060 回答