How are mysql multi-field OR queries sorted?

existing table user

data structure
id name sex age
1 B 2 20
2 A 2 25
3 B 1 20
4 A 1 30

select * from user where name ="A"or sex ="1" can find data

2 A 2 25
3 B 1 20
4 A 1 30

from the point of view of the above data, the data with an id of 4 have the highest degree of matching. How to sort the data with the highest degree of matching (multi-fields).

Mar.22,2021

in fact, this problem can be regarded as a mathematical set problem
your sql:

select * from user where name = 'A' or sex = '1' 

can be equivalent to the following sql

select * from user where name = 'A' AND sex = 1 
UNION ALL
SELECT * FROM user WHERE name = 'A' AND sex != 1
UNION ALL
SELECT * FROM user WHERE name != 'A' AND sex = 1

split into three parts, you can freely adjust the display order
Thank you.

Menu