goldenfish
goldenfish

Reputation: 49

How to order rows alpabetically in MySQL?

I have the following query in mysql:

SELECT DISTINCT re1.name as name1, re2.name as name2
FROM Rating ra1 JOIN Reviewer re1
ON re1.rID = ra1.rID
CROSS JOIN
Reviewer re2 JOIN Rating ra2 ON re2.rID = ra2.rID
WHERE ra1.mID = ra2.mID
AND re1.rID != re2.rID
AND re1.rID > re2.rID
ORDER BY name1, name2;

This is the retrieved data:

enter image description here

How can I order the obtained rows alphabetically? (ie, in second row, to switch Brittany and Chris Position)

Thanks

Upvotes: 1

Views: 44

Answers (1)

forpas
forpas

Reputation: 164099

Use the functions LEAST() and GREATEST():

SELECT DISTINCT 
       LEAST(re1.name, re2.name) AS name1, 
       GREATEST(re1.name, re2.name) AS name2
FROM ...

Upvotes: 1

Related Questions