PDA

View Full Version : Query question


Tacomon
05-07-2006, 12:51 AM
Here's my two tables with the relevant fields

TABLE 1: user_id | user_name
TABLE 2: event_id | event_winner (a user_id from Table 1) | event_loser (a user_id from Table 1)

I would like to do a query with the information needed to output the last ten events and the names of the winners and losers. Trying:

SELECT table1.user_name, table2.event_winner, table2.event_loser
FROM table1
INNER JOIN table2
ON table1.user_id = table2.event_winner
OR table1.user_id = table2.event_winner

This partly did what I wanted, but separated the winner on one line and the loser on the next line of the results. Adding GROUP BY alleviates this, but gets rid of one of the names.

How do I get both the winner and loser's name on the same line of results? Thanks for any help. :)

GJay
05-07-2006, 09:57 AM
for the sake of clarity, I'm going to assume your tables are called 'users' and 'events':

SELECT e.event_id,w.username AS winner,l.username AS loser FROM events e JOIN users w ON (e.event_winner=w.user_id) JOIN users l ON (e.event_loser=l.user_id) ORDER BY e.event_id DESC LIMIT 10

is untested, but should be close...

Tacomon
05-08-2006, 08:48 AM
Thanks so much. That did it.