Union & ORDER BY?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
4 views
Good time of day!


There is a table:

idnamegood_namepayment
1aaatrue
2bbbpayment
3ccctrue



You must choose paid in and sort them by product (good_name) then go unpaid, and they need to sort by company name (name).


I tried using union:

(SELECT * FROM `table` WHERE `payment`=true ORDER BY `good_name` ASC )
union
(SELECT * FROM `table` WHERE `payment`= 'false' ORDER BY `name` ASC )



How do you implement this perversion?..


PS Thanks corrected the error.
by | 4 views

6 Answers

0 like 0 dislike
a possible
\r
\r
SELECT *, IF(`payment`='true', `good_name`, `name`) prior FROM `table` WHERE `payment`="true" ORDER BY payment, prior 
by
0 like 0 dislike
SELECT * FROM test ORDER BY payment, CASE WHEN payment = 1 THEN good_name ELSE NAME end
by
0 like 0 dislike
SELECT * FROM `table` ORDER BY `payment` DESC, `good_name` ASC
by
0 like 0 dislike
SELECT * FROM ( SELECT * FROM `table` WHERE `payment`=="true" ORDER BY `good_name` ASC ) a UNION select * from ( SELECT * FROM `table` WHERE `payment`== 'false' ORDER BY `name` ASC ) b 

so will work, but you're right — it is a perversion
by
0 like 0 dislike
For syntax I do not warrant, but the point is simple — do the Select from the generalized table with additional fields:
\r
SELECT id, name, good_name, payment
\rFROM (SELECT TOP (100) PERCENT id, name, good_name, payment, good_name AS ordr
\r FROM temp
\r WHERE (payment = 1)
\r UNION
\r SELECT TOP (100) PERCENT id, name, good_name, payment, name AS ordr
\r FROM temp AS temp_1
\r WHERE (payment = 0)) AS t2_1
\rORDER BY payment DESC, ordr

\r
\r* This source code was highlighted with Source Code Highlighter.
by
0 like 0 dislike
what is a DBMS needs to write WHERE `payment`=="true" is IS TRUE and even with zhivotnym the same? Please do not kick — in addition to ORACLE almost did not see any for the last couple of years and trying to use ANSI.
\r
my version: SELECT t.*, decode(payment, true, good_name, name) as ord FROM table as t ORDER BY ord
no better than the variant with an IF/CASE
by

Related questions

0 like 0 dislike
1 answer
asked Jun 5, 2019 by alexandrryabchuk
0 like 0 dislike
3 answers
0 like 0 dislike
3 answers
asked Apr 11, 2019 by Set89
0 like 0 dislike
3 answers
0 like 0 dislike
3 answers
110,608 questions
257,186 answers
0 comments
27,959 users