Nick M
Nick M

Reputation: 2532

SQL help about selecting 3 rows in a specific order

I'm trying to run a SQL query that will always return either 2 or 3 rows; when 3 are returned, got to sort them like this: team1, draw, team2

So basically if "draw" is in the list, then it should always be placed between the other two rows whatever they are, assuming the name of the teams and draw are in column "title"

I have tried ordering by CASE WHEN title = 'draw' THEN 1, also tried FIELD() - no luck :(

Could anyone help?

Thank you!

Upvotes: 0

Views: 60

Answers (2)

Lajos Arpad
Lajos Arpad

Reputation: 76621

Teams differ from results. I believe you should not solve your problem. Instead, create three tables: Teams, Results and ResultTypes. In ResultTypes store any possible result between two teams. In Teams, store the teams, their names and any relevant data to them. Finally, in Results store 3 Foreign keys. Two of them should reference to the Teams table and one to the ResultTypes table. This way you will never have problems with selecting data from your database.

Upvotes: 0

sashkello
sashkello

Reputation: 17871

Try this:

ORDER BY (IF(title='team1', 1, IF(title='team2', 3, 2)))

Upvotes: 1

Related Questions