Aitor Ramos Pajares
Aitor Ramos Pajares

Reputation: 361

No order in MySQL query

I have this query

Select nametwo
from cities
inner join usuarios
where cities.nametwo=usuarios.jug1
   or cities.nametwo=usuarios.jug2
   or cities.nametwo=usuarios.jug3

and the data is

jug1 = 2 jug2 = 1 jug3 = 4

then, the query order the data by ASC, and I want that order it by the order of the select. is there a way for do this? thanks

Upvotes: 1

Views: 417

Answers (2)

EternalHour
EternalHour

Reputation: 8631

Seems that you are wanting to order by the usuarios table unless I am misunderstanding.

Select nametwo
from cities
inner join usuarios
where cities.nametwo=usuarios.jug1
or cities.nametwo=usuarios.jug2
or cities.nametwo=usuarios.jug3
order by usuarios

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1270401

First, if you use inner join, use an on clause. This is required in every database except MySQL and it just looks really awkward. Here is an equivalent query:

Select c.nametwo
from cities c inner join
     usuarios u
     on c.nametwo in (u.jug1, u.jug2, u.jug3);

This formulation actually directly suggests the answer. Use the field() function:

order by field(c.nametwo, u.jug1, u.jug2, u.jug3)

Upvotes: 0

Related Questions