tags:

views:

174

answers:

5

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.

+2  A: 

SELECT DISTINCT ...

Stephen Denne
+2  A: 

If I understood this right, using SELECT DISTINCT instead of SELECT will solve your problem

soulmerge
+5  A: 

I would advice to use SELECT DISTINCT, but I rather ask you this question:

Given the tables:

AUCTION     USERNAME
--------    -------
Sotheby's   john
Christie's  john

USERNAME   FIRSTNAME LASTNAME
--------   --------  --------
john       John      Doe
john       John      Davis

, what do you want to be selected for Sotheby's and what for Christie's?

It's impossible to understand what do you mean by "a way to run a query limiting to 1 record per username" unless you answer this question.

Update:

SELECT  *
FROM    users
WHERE   username IN
        (
        SELECT  username
        FROM    auctions
        WHERE   category = 'category3'
        )

Update 2:

Does this query return something?

SELECT  u.username, u.firstname, u.lastname
FROM    auctions a, users u
WHERE   a.category = 'fake'
        AND u.username = a.username

Update 3:

SELECT  ao.username, u.firstname, u.lastname
FROM    (
        SELECT  DISTINCT username
        FROM    auctions a
        WHERE   category = 'fake'
        ) ao
LEFT JOIN
        users u
ON      u.username = a.username
Quassnoi
Aye, I just put that as a comment on his original post.
Kezzer
I have edited my question to answer this
Joshxtothe4
I have made another update
Joshxtothe4
It returns: #1052 - Column 'username' in field list is ambiguous
Joshxtothe4
ahh, it still returned a 0 result set
Joshxtothe4
This means there are no actual users for the 'fake' category. Select all usernames from auctions in 'fake' category and make sure it's the case. The query in `Update` actually does what you need.
Quassnoi
Most DBMS would require either a.username or u.username in the SELECT list, even though they're equivalent. Update 2 certainly looks as though it should return the correct data - are you sure your data is as you say? What do you get from SELECT COUNT(*) FROM Auctions WHERE category = 'fake'?
Jonathan Leffler
SELECT COUNT(*) FROM AUCTIONS WHERE category = 'fake' returns 5.
Joshxtothe4
Most probably, all username's are NULL for the auctions in the fake category, as nobody bothered to fill them.
Quassnoi
Issue SELECT username FROM AUCTIONS WHERE category = 'fake' and see what the USERNAME's are. Then try to find these USERNAME's in actual USERS table.
Quassnoi
Also try to run the query in `Update` against a real category, not the fake one.
Quassnoi
The fake category is a real one..it is to do with fraudulent brands. The USERS table does not contain equivalent usersnames..I should have seen that straight of. That's why I want to grab the username from AUCTIONS and the rest of the data, filled in or not from USERS.
Joshxtothe4
So what do you want to be returned for the 'fake' category?
Quassnoi
I just have a column named category in auctions, which can be set to whatever I like. I want to select the username from auctions where category is equal to a paramter I choose and that exists, and then select the fields from users for the same username.
Joshxtothe4
To do this, I guess the username field in users would have to be filled in?
Joshxtothe4
You didn't answer my question. You have a fake category in your table, with 5 records in it. Provided that no username from these records is actually contained in 'USERS', how many records do you want to be returned and what should be contained in these records?
Quassnoi
I would want only the username from the fake category in auctions returned, and the blank fields from users returned
Joshxtothe4
So you want five records with blank fields? Or one record with blank fields?
Quassnoi
seemingly Bingo! Many thanks!
Joshxtothe4
hmm, still duplicates. I am using SELECT ao.USERNAME, u.firstname, u.lastname FROM ( SELECT DISTINCT USERNAME FROM AUCTIONS a WHERE category = 'fake' )ao LEFT JOIN USERS u ON u.username = ao.USERNAME WHERE upper( ao.USERNAME ) LIKE '%b%' LIMIT 0 , 10 which gives a username twice
Joshxtothe4
It seems to be based on how many records are in the USERS table
Joshxtothe4
You said in your post: 'usernames are always unique'. Is it really so?
Quassnoi
I mean, are usernames unique in table USERS? If not, then answer the question I asked in the very beginning.
Quassnoi
A: 

English is not my native language but, if I understand correctly, you want to select all users (once) who has auction in given category x.

I am not sure if this syntax works for MySQL, but try something like:

SELECT
    AUCTIONS.USERNAME,
    USERS.firstname,
    USERS.lastname,
    USERS.flaggedauctions
FROM
(
    SELECT DISTINCT 
     AUCTIONS.USERNAME
    FROM
     AUCTIONS
    WHERE
     AUCTIONS.CATEGORY = x
) AS AUCTIONS
INNER JOIN 
    USERS
ON 
    USERS.USERNAME = AUCTIONS.USERNAME
Grzegorz Gierlik
A: 

You can simply use

SELECT DISTINCT
    a.username,
    u.firstname,
    u.lastname
FROM 
    Users u
RIGHT JOIN
    Auctions a
    ON
    u.username = a.username
WHERE
    a.category = 'category3'
Russ Cam