I am doing a project using MySQL 5. The requirement is the following:
Give the user names, device types, OS version and fruit involved in picks, where users had the
same device type, were running iOS 4 or 4.1, and picked the same fruit
The relevant tables are as follows:
User: {uID: INT, name: VARCHAR(45), deviceOS: VARCHAR(45), deviceType: VARCHAR(45)}
Pick: {uID: INT, ts: TIMESTAMP, fruit: VARCHAR(45)}
(Primary keys in bold. uID in Pick is a foreign key of uID in User.)
I am doing the following query:
SELECT DISTINCT NAME1, OS1, DEV1, NAME2, OS2, DEV2, P1.fruit FROM Pick AS P1, Pick AS P2,
(SELECT U1.uID AS User1, U1.name AS NAME1, U1.deviceOS AS OS1, U1.deviceType AS DEV1,
U2.uID AS User2, U2.name AS NAME2, U2.deviceOS AS OS2, U2.deviceType AS DEV2
FROM User AS U1, User AS U2
WHERE (U1.uID != U2.uID) AND
(U1.deviceType = U2.deviceType) AND
(U1.deviceOS = "4" OR U1.deviceOS = "4.1") AND
(U2.deviceOS = "4" OR U2.deviceOS = "4.1")) AS PartialResult
WHERE (P1.uID = PartialResult.User1) AND
(P2.uID = PartialResult.User2) AND
(P1.fruit = P2.fruit)
This returns the following result, but as you see, it is in some way "duplicated":

I have tried solving this using GROUP BY fruit but it will not return the correct result on the general case. Limit 1 also would not work on the general case. So after numerous hours trying to figure this out, I must ask:
Is there a way to prevent this kind of duplication on the general case?
Thank you!
Instead of
U1.uID != U2.uID, writeU1.uID > U2.uID.