2015-04-23 50 views
0

我需要返回姓氏以M-Z開頭的所有人的全名。大寫是正確的情況。基本的MySQL選擇查詢

Select full_name 
from customer 
where lastname like '[M-Z]%' 
order by lastname; 

我的語法在第三個像我有什麼問題,我不知道它是什麼。

回答

0

你可以用正則表達式來做到這一點。

Select full_name from customer where lastname REGEXP '^[M-Z]' order by lastname; 
0
select full_name from customer where substring(lastname,1,1) >= 'M' and substring(lastname,1,1) <='Z' order by lastname; 

select full_name from customer where substring(lastname,1,1) between 'M' and 'Z' order by lastname; 
0

下面的代碼將工作以及:

SELECT fullname FROM customer 
WHERE lastname BETWEEN (
    SELECT lastname FROM customer 
    WHERE lastname LIKE 'M%' ORDER BY lastname ASC LIMIT 1) 
AND (
    SELECT lastname FROM customer 
    WHERE lastname LIKE 'Z%' ORDER BY lastname DESC LIMIT 1 
);