# 两个LIKE条件，但先从一个条件检索，然后从另一个条件检索Two LIKE conditions but retrieving from one first then the other

``````   SELECT *
WHERE name LIKE 'a%' OR name LIKE '% a%' LIMIT 10
``````

## 4 个回复4

### ===============>>#1 票数：3 已采纳

``````SELECT *
WHERE name LIKE 'a%' OR name LIKE '% a%'
ORDER BY CASE WHEN name LIKE 'a%' THEN 0 ELSE 1 END
LIMIT 10
``````

### ===============>>#2 票数：0

``````SELECT t1.*
FROM (
SELECT *
WHERE name LIKE 'a%'
LIMIT 10
) t1
WHERE t1.name LIKE '% a%'
``````

### ===============>>#3 票数：0

``````ORDER BY IF(name LIKE 'a%',1,2)
``````

``````SELECT *
WHERE name LIKE 'a%' OR name LIKE '% a%'
ORDER BY IF(name LIKE 'a%',1,2)
LIMIT 10
``````

``````SELECT c.*
FROM ( SELECT a.*
FROM (
SELECT *
WHERE name LIKE 'a%'
LIMIT 10
) a
UNION ALL
SELECT b.*
WHERE b.name LIKE '% a%' AND b.name NOT LIKE 'a%'
LIMIT 10
) c
ORDER BY c.name LIKE 'a%' DESC
LIMIT 10
``````

### ===============>>#4 票数：0

``````(SELECT 1 AS sort_col, col1a, col1b, ... FROM t1)
UNION
(SELECT 2, col2a, col2b, ... FROM t2) ORDER BY sort_col;
``````

``````(Select *, 1 as sortcol from addresses where name like 'a%')
Union
(Select *, 2 as sortcol from addresses where name like '% a%')
Order by sortcol
``````