brad
2007-02-03 19:44:24 UTC
Scenario:
Voting system where one chooses one item over another. My votes table
has a winner_id and a loser_id. I need to determine what the popular
match ups are.
This is my query so far:
SELECT winner_id, items1.name as name1, loser_id, items2.name as
name2, count(*) as popularity
FROM votes
INNER JOIN items items1 ON items1.id=winner_id
INNER JOIN items items2 ON items2.id=loser_id
GROUP BY winner_id, loser_id
ORDER BY popularity
DESC LIMIT 5
The problem is this query considers "Item A and Item B" to be
different from "Item B and Item A" where I need them to be considered
as the same thing.
Is it possible to write a query that can do this or a better way to
structure the tables to make the first query work? I'm totally lost,
I don't even know where to look! Please help!
Thanks in advance,
Brad.
Voting system where one chooses one item over another. My votes table
has a winner_id and a loser_id. I need to determine what the popular
match ups are.
This is my query so far:
SELECT winner_id, items1.name as name1, loser_id, items2.name as
name2, count(*) as popularity
FROM votes
INNER JOIN items items1 ON items1.id=winner_id
INNER JOIN items items2 ON items2.id=loser_id
GROUP BY winner_id, loser_id
ORDER BY popularity
DESC LIMIT 5
The problem is this query considers "Item A and Item B" to be
different from "Item B and Item A" where I need them to be considered
as the same thing.
Is it possible to write a query that can do this or a better way to
structure the tables to make the first query work? I'm totally lost,
I don't even know where to look! Please help!
Thanks in advance,
Brad.