I would like to order my results of a query following the order of where clause.
For example, i have this query :
SELECT *
FROM config_ban_num
WHERE
( adresse_seo LIKE '12-rue-de-rivoli%'
OR adresse_seo LIKE '13-rue-de-rivoli%'
OR adresse_seo LIKE '11-rue-de-rivoli%' )
AND (postcode='75004')
When i print the results, i've got this :
11-rue-de-rivoli
12-rue-de-rivoli
13-rue-de-rivoli
I would like this order regarding the order in where clause :
12-rue-de-rivoli
13-rue-de-rivoli
11-rue de rivoli
One option is using a subquery:
SELECT * FROM (
SELECT *,
CASE WHEN adresse_seo LIKE '12-rue-de-rivoli%' THEN 1
WHEN adresse_seo LIKE '13-rue-de-rivoli%' THEN 2
WHEN adresse_seo LIKE '11-rue-de-rivoli%' THEN 3
ELSE 4 END as Position
FROM config_ban_num
WHERE
( adresse_seo LIKE '12-rue-de-rivoli%'
OR adresse_seo LIKE '13-rue-de-rivoli%'
OR adresse_seo LIKE '11-rue-de-rivoli%' )
AND (postcode='75004')
) SubQuery
ORDER BY Position
Use MySQL's FIELD function :
SELECT *
FROM config_ban_num
WHERE adresse_seo in ('12-rue-de-rivoli%', '13-rue-de-rivoli%', '11-rue-de-rivoli%')
AND (postcode='75004')
ORDER BY FIELD(adresse_seo, '12-rue-de-rivoli%', '13-rue-de-rivoli%', '11-rue-de-rivoli%')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.