How to do sorting in sql?

make such a request
(SELECT * FROM users
INNER JOIN predprijatie ON users.mesto_rab = predprijatie.pred_id
WHERE user_visible=1 AND parent_id=0 AND users_id = 34)
UNION
(SELECT * FROM users
INNER JOIN predprijatie ON users.mesto_rab = predprijatie.pred_id
WHERE user_visible=1 AND parent_id=0 AND users_id <> 34
ORDER BY name ASC , ASC mesto_rab )


the first select picks out only one line and puts it first in the results , the second query selects all the rows except the first and sort on two columns

the problem is that the sorting takes place and on a moment's notice , too, how not to sort the first query?
March 20th 20 at 11:27
2 answers
March 20th 20 at 11:29
Solution
SELECT * FROM users
INNER JOIN predprijatie ON users.mesto_rab = predprijatie.pred_id
WHERE user_visible=1 AND parent_id=0
ORDER BY CASE WHEN users_id = 34 THEN 0 ELSE 1 END, name, mesto_rab
under your dialect only adjust - rees commented on March 20th 20 at 11:32
it works but does not sort by mesto_rab - tyson.Haley commented on March 20th 20 at 11:35
@dewayne.OReilly, will sort in the same name as specified in the original request. well, or sort order will change to the one You need. - rees commented on March 20th 20 at 11:38
@Dulce.McGlynn87, do not quite understand , if I do sorting by mesto_rab first , then name alphabetically will not be sorted , how can we do a double sort?
I earlier when did not choose 34 id worked with two sorts - tyson.Haley commented on March 20th 20 at 11:41
March 20th 20 at 11:31
Solution
SELECT 0, name, mesto_rab, * FROM users
INNER JOIN predprijatie ON users.mesto_rab = predprijatie.pred_id
WHERE (user_visible=1) AND (parent_id=0) AND (users_id = 34)
UNION
SELECT 1, name, mesto_rab, * FROM users
INNER JOIN predprijatie ON users.mesto_rab = predprijatie.pred_id
WHERE (user_visible=1) AND (parent_id=0) AND (users_id <> 34)
ORDER BY 1, 2 ASC, 3 ASC

Or without UNION
SELECT * FROM users
INNER JOIN predprijatie ON users.mesto_rab = predprijatie.pred_id
WHERE (user_visible=1) AND (parent_id=0)
ORDER BY CAST( users_id - 34 AS BIT), ASC name , ASC mesto_rab
#1064 - you Have an error in the query. Refer to the documentation for the version of MySQL on the subject of correct syntax near '* FROM users
INNER JOIN predprijatie ON users.mesto_rab = predprijatie.pred_id
'at line 1 - tyson.Haley commented on March 20th 20 at 11:34
with union adjusted under itself , all works but no sorting by mesto_rab

,
and the second
#1064 - you Have an error in the query. Refer to the documentation for the version of MySQL on the subject of correct syntax near 'BIT), name ASC , mesto_rab ASC LIMIT 0, 25' at line 4
- tyson.Haley commented on March 20th 20 at 11:37

Find more questions by tags MySQL