S
S
Sergey Korenevsky2020-04-03 02:19:49
openvpn
Sergey Korenevsky, 2020-04-03 02:19:49

How to sort a table by matching multiple rows?

Dear GURU and pro SQL, give advice please.
There is a sequence of lines: "Table", "Chair", "Puff".
As you can see, the sequence is not in alphabetical order,
So here's how to sort the table so that 1/3 are Tables, 1/3 Chairs, 1/3 Ottomans
And so that they are displayed only in this order.

SELECT * FROM mebel  WHERE type="Стол"
UNION
SELECT * FROM mebel  WHERE type="Стул"
UNION
SELECT * FROM mebel  WHERE type="Пуфик"

Is it possible to do this somehow with one query, more simply, using ORDER BY for example?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander, 2019-09-24
@Whey

Forcing you to watch a video is not a good person)))
1.

remote here ip
- IP address of the router hopefully. In the video, for example, 176.122.115.66 is indicated. This IP is issued by the provider (white IP) for your ASUS.
2. I hope the provider does not cut ports)))
UPD
3. push "route 192.168.0.0 255.255.255.0" replace push "route 192.168.1.0 255.255.255.0"

O
oia, 2019-09-24
@oia

do you have white or grey?
true grey! and the gray ip for the internet has no routing

S
Sergey Korenevsky, 2020-04-03
@Dier_Sergio_Great

found.

SELECT * FROM mebel  
ORDER BY 
    CASE type
        WHEN 'Стол' THEN 1
        WHEN 'Стул' THEN 2
        WHEN 'Пуфик' THEN 3
        ELSE 4
    END ASC, 
    name ASC;

SELECT * FROM mebel  
ORDER BY FIELD(type, 'Стол', 'Стул', 'Пуфик');

The trick is that those lines that are not the type of these three, they will be shown at the beginning of the list. because FIELD returns 0 if out of the three it doesn't match.
second option
SELECT * FROM mebel  
ORDER BY FIND_IN_SET(type, 'Стол,Стул,Пуфик');

And I'm writing this here for curiosity.
Smart sorting for a field with values
​​"1-A", "10-A", "3-A", "33-A"
SELECT *
FROM mebel
ORDER BY CAST(name AS  UNSIGNED), name;

natsort($array);//аналогично для PHP

M
mayton2019, 2020-04-03
@mayton2019

See how it works
ORDER BY ISNULL(field)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question