Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql: select * where first and last name starts with the same letter

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

- this selects people with with the same first and last name, but that's for m only. How to select all such people from a to z (order by desc is not a matter)?

like image 285
Haradzieniec Avatar asked May 31 '26 18:05

Haradzieniec


2 Answers

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

Explanation: takes the leftmost 1 character of first name and of last name, converts them to uppercase, and compares them.

like image 148
Shai Avatar answered Jun 03 '26 11:06

Shai


SELECT * FROM people WHERE LEFT(first_name, 1) = LEFT(last_name, 1);
ORDER BY last_name, first_name
like image 38
Jack Avatar answered Jun 03 '26 13:06

Jack