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.
zenna