SQL, How to request multiple foreign keys in a table?

I have a project table in which there are two foreign keys for users (user_id and winner_user_id), one for the project owner and one for the project winner. Sort of

+----------------+-------------------------+------+-----+---------+----------------+
| 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'm trying in one request to get information about projects and data about all 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

Obviously returns an empty set. The real problem is how can I reference these different user_id. I even tried using the AS keyword, and then used the name that I created in the same sql query, but apparently this does not work.

To make everything clear, 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 build a request to handle this?

Thanks in advance.

+5
5

, , . , .

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
+8
SELECT ... FROM users AS winers, users AS owners 
WHERE projects.user_id=owners.user_id
        AND projects.winner_user_id=winners.user_id
+2

, - :

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, btw.

. , , .

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

NULL, ( winner_user_id 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
0
source

All Articles