基本MySQL选择查询

时间:2015-04-23 05:07:46

标签: mysql sql database

我需要返回姓名以M-Z开头的所有人的全名。大写是正确的情况。

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

我的第三方语法有问题,我无法弄清楚它是什么。

4 个答案:

答案 0 :(得分:0)

you can do this with regular expression.

Select full_name from customer where lastname REGEXP '^[M-Z]' order by lastname; 

答案 1 :(得分:0)

I suggest the use of BETWEEN.

SELECT full_name
FROM customer
WHERE lastname BETWEEN 'M%' AND 'Z%'
ORDER BY lastname;

See: http://www.w3schools.com/sql/sql_between.asp

答案 2 :(得分:0)

select full_name from customer where substring(lastname,1,1) >= 'M' and substring(lastname,1,1) <='Z' order by lastname;

or

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

答案 3 :(得分:0)

Below code would work as well:

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
);