M
M
musclecode2019-07-01 00:41:40
MySQL
musclecode, 2019-07-01 00:41:40

How to get data from database in order?

Select * from tablename where model = 'A' or model = 'B'
with such a record, the result is unpredictable, 'B' can be found before 'A', the array is located like this ['B', 'A'], but you need [' A', 'B']

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bkosun, 2019-07-01
@musclecode

SELECT * FROM `table_name` WHERE `column_name` IN ('A', 'B', 'C') ORDER BY FIELD (`column_name`, 'A', 'B', 'C')

https://dev.mysql.com/doc/refman/8.0/en/string-fun...
https://dev.mysql.com/doc/refman/8.0/en/string-fun...

M
musclecode, 2019-07-01
@musclecode

I don’t know if this is effective or not, but, it seems, I managed to solve it like this:
select * from tablename where model = 'A' union select * from tablename where model = 'B',
you need the order to be exactly as specified in the query

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question