SQL,如何查询表中的多个外键?

时间:2009-09-12 17:19:23

标签: sql mysql database

我有一个项目表,其中有两个用户外键(user_id和winner_user_id),一个用于项目所有者,另一个用于项目的获胜者。像

这样的东西
+----------------+-------------------------+------+-----+---------+----------------+
| Field          | Type                    | Null | Key | Default | Extra          |
+----------------+-------------------------+------+-----+---------+----------------+
| project_id     | int(10) unsigned        | NO   | PRI | NULL    | auto_increment | 
| start_time     | datetime                | NO   |     | NULL    |                | 
| end_time       | datetime                | NO   |     | NULL    |                | 
| title          | varchar(60)             | NO   |     | NULL    |                | 
| description    | varchar(1000)           | NO   |     | NULL    |                | 
| user_id        | int(11)                 | NO   |     | NULL    |                | 
| winner_user_id | int(10) unsigned        | YES  |     | NULL    |                | 
| type           | enum('fixed','auction') | YES  |     | NULL    |                | 
| budget         | decimal(10,0)           | YES  |     | NULL    |                | 
+----------------+-------------------------+------+-----+---------+----------------+

现在,我正在尝试使用单个查询来获取有关项目和有关两个用户的数据的信息。

所以我制定了像

这样的查询
SELECT projects.project_id, projects.title, projects.start_time,
            projects.description, projects.user_id, projects.winner_user_id,
            users.username as owner, users.username as winner
        FROM projects,users   
        WHERE projects.user_id=users.user_id
        AND projects.winner_user_id=users.user_id

显然返回一个空集。真正的问题是如何引用这些不同的user_id。我甚至尝试使用AS关键字,然后使用我在同一个sql查询中创建的名称,但显然这不起作用。

最后要说清楚,我想要像

这样的东西
+------------+-------------------------------------------------+---------------------+---------+----------------+--------------+--------------+
| project_id | title                                           | start_time          | user_id | winner_user_id | owner        | winner       |
+------------+-------------------------------------------------+---------------------+---------+----------------+--------------+--------------+
|          1 | CSS HTML Tableless expert for site redesign     | 2009-09-01 21:07:26 |       1 |              3 | mr X        | mr Y        | 
|          2 | High Quality Ecommerce 3-Page Design HTML & CSS | 2009-09-01 21:10:04 |       1 |              0 | mr X        | mr Z        | 

如何构建查询来处理此问题?

提前致谢。

5 个答案:

答案 0 :(得分:8)

你很接近,但你需要两次加入用户表,一次是在拥有者身上,一次是在获胜者身上。使用表别名来区分这两者。

SELECT 
      projects.project_id
    , projects.title
    , projects.start_time
    , projects.description
    , projects.user_id
    , projects.winner_user_id
    , users.username as owner
    , winnerUser.username as winner
FROM projects
INNER 
    JOIN users
    ON projects.user_id=users.user_id
INNER 
    JOIN users winnerUser
    ON projects.winner_user_id=winnerUser.user_id

答案 1 :(得分:2)

SELECT ... FROM users AS winers, users AS owners 
WHERE projects.user_id=owners.user_id
        AND projects.winner_user_id=winners.user_id

答案 2 :(得分:1)

使用这样的东西怎么样:

SELECT projects.project_id, projects.title, projects.start_time,
    projects.description, projects.user_id, projects.winner_user_id,
    user_owner.username as owner, user_winner.username as winner
FROM projects
    inner join users user_owner on user_owner.user_id = projects.user_id
    inner join users user_winner on user_winner.user_id = projects.winner_user_id

您首先拥有该项目,然后您在所有者(使用一个特定别名)上进行内部联接,然后在获胜者上使用内部联接(使用其他特定别名)

并且,在select子句中,您使用这些别名来获取所需的信息 - 如果您需要限制where子句中的任何内容,则相同,顺便说一句。

注意:如果您还想要没有获胜者的项目,您可能希望使用左连接而不是内连接。

答案 3 :(得分:0)

SELECT u1.user_id AS user_id, u2.user_id AS AS winner_id 
FROM projects p
    INNER JOIN users u1 ON p.user_id=u1.user_id
    INNER JOIN users u2 ON p.winner_user_id=u2.user_id

答案 4 :(得分:0)

这应该有效,如果获胜者为unknown(当winner_user_id为null时),则返回NULL

SELECT projects.project_id, 
  projects.title, 
  projects.start_time,
  projects.description, 
  projects.user_id, 
  projects.winner_user_id,
users_owner.username as owner, 
users_winner.username as winner
FROM projects
  INNER JOIN users AS users_owner ON users_owner.user_id = projects.user_id
  LEFT OUTER JOIN users AS users_winner ON users_winner.user_id = projects.winner_user_id