I have two tables, auctions and users.
I want to select the username table from auctions where category=x, and then select the fields a, b and c from the users table, where the username field in users matches th username fields from auctions.
I am trying this:
SELECT AUCTIONS.USERNAME, USERS.firstname, USERS.lastname, USERS.flaggedauctions
FROM AUCTIONS
INNER JOIN USERS
ON AUCTIONS.USERNAME=USERS.USERNAME
Which seems to work. However, USERNAME is not a primary key in either table, and in the auctions table there can be many records with the same username, as where there will be only one record per username in the users table.
The above query works, but the problem arises if I want to limit the result set to say 10, for pagination. This may result in 10 records returned, some of which are duplicates. Is there a way to run a query limiting to 1 record per surname inside of another query?
edit: in answer to Quassnoi's post
usernames are always unique
If I have
Auctions:
username category blah
-------------------------------------
user1 category1 tshirt
user2 category2 jeans
user3 category3 shoes
user2 category3 belt
user3 category3 pants
Users:
username firstname lastname
-------------------------------------
user1 john smith
user2 fred black
user3 alice brady
Then given category 3 as the category, I would want to show:
username firstname lastname
-------------------------------------
user2 fred black
user3 alice brady
With username coming from the auctions table.
Instead, at the moment this will display:
username firstname lastname
-------------------------------------
user2 fred black
user3 alice brady
user3 alice brady
edit2:
I am using
SELECT username, firstname, lastname
FROM USERS
WHERE username
IN (
SELECT USERNAME
FROM AUCTIONS
WHERE category = 'fake'
)
LIMIT 0 , 30
Which returns 0 results. There are certainly many records in AUCTIONS with category set to fake.