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

时间:2022-07-16 11:29:34

I have a projects table which has two foreign keys for users (user_id and winner_user_id), one for the owner of the project and one for the winner of the project. Something like

我有一个项目表,其中有两个用户外键(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    |                | 
+----------------+-------------------------+------+-----+---------+----------------+

Now I am trying in a single query to get information about projects and the data about both of the users.

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

So I formulated a query like

所以我制定了一个类似的查询

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

Which returns an empty set obviously. The real problem is how do I reference these different user_ids. I even tried using the AS keyword and then using the name I had created in the same sql query but apparently that doesn't work.

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

To make things clear in the end I would like something like

为了使事情最终清楚我想要的东西

+------------+-------------------------------------------------+---------------------+---------+----------------+--------------+--------------+
| 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        | 

How can I construct a query to handle this?

如何构造查询来处理这个问题?

Thanks in advance.

提前致谢。

5 个解决方案

#1


8  

You are close, but you need to join the user table in twice, once on the owner and once on the winner. Use a table alias to differentiate the two.

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

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

#2


2  

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

#3


1  

What about using something like this :

怎么样使用这样的东西:

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

You first have the project, then you inner join on the owner (using one specific alias), and then inner join on the winner (using another specific alias).

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

And, in the select clause, you use those aliases to get the information you want -- same if you needed to restrict anything in a where clause, btw.

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

Note : if you also want projects which don't have a winner yet, you might want to use a left join instead of an inner.

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

#4


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

#5


0  

This should work, and return NULL if the winner in unknown (when winner_user_id is null)

这应该工作,如果获胜者在未知(当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

#1


8  

You are close, but you need to join the user table in twice, once on the owner and once on the winner. Use a table alias to differentiate the two.

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

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

#2


2  

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

#3


1  

What about using something like this :

怎么样使用这样的东西:

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

You first have the project, then you inner join on the owner (using one specific alias), and then inner join on the winner (using another specific alias).

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

And, in the select clause, you use those aliases to get the information you want -- same if you needed to restrict anything in a where clause, btw.

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

Note : if you also want projects which don't have a winner yet, you might want to use a left join instead of an inner.

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

#4


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

#5


0  

This should work, and return NULL if the winner in unknown (when winner_user_id is null)

这应该工作,如果获胜者在未知(当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