Osman
Osman

Reputation: 1771

select * where distinct

I am trying to do something like this:

select * from receipt where branch=:branch AND distinct transactionid

However this is not working and brings up all results. I just need to get all the information that matches what I need and has a distinct transaction. So that might include firstname lastname and all the information that each listing in the database stores as I use all that information later.

Lets say I have:

+--------+-----------+----------+-----------------+
| first  |  last     |  item    |  transactionid  |
+--------+-----------+----------+-----------------+
| jack   |  sparrow  |  water   |    1234         |
| jack   |  sparrow  |  food    |    1234         |
| jack   |  sparrow  |  wood    |    1234         |
+--------+-----------+----------+-----------------+

I want all listings with the first name jack (any one of them) with a distinct transactionid...

Can this even be done?

Upvotes: 1

Views: 959

Answers (1)

John Woo
John Woo

Reputation: 263733

try this one,

SELECT  a.*
FROM    tableName a
        INNER JOIN
        (
            SELECT  transactionID, MIN(item) minItem
            FROM    tableName
            GROUP BY transactionID
        ) b ON a.transactionID = b.transactionID AND
                        a.item = b.minItem
// -- WHERE branch =: branch

Thanks for the edit zerkms

so what does the subquery do?

Well, it just gets one item per transactionID. You can also use MAX on it.

Upvotes: 4

Related Questions