mysql:select *其中名字和姓氏以相同的字母开头

时间:2013-11-05 21:28:11

标签: mysql sql select where

SELECT * FROM `people` WHERE first_name like 'm%' and last_name like 'm%';

- 这会选择具有相同名字和姓氏的人,但仅适用于m。如何从a到z选择所有这样的人(按desc命令不是问题)?

3 个答案:

答案 0 :(得分:5)

SELECT * FROM `people` WHERE UPPER(LEFT(first_name, 1)) = UPPER(LEFT(last_name, 1))

说明:获取名字和姓氏最左边的1个字符,将它们转换为大写,然后比较它们。

答案 1 :(得分:3)

SELECT * FROM people WHERE LEFT(first_name, 1) = LEFT(last_name, 1);
ORDER BY last_name, first_name

答案 2 :(得分:2)

select *
 from people
 where substring(first_name,1,1) = substring(last_name,1,1)
 order by last_name, first_name