Haradzieniec
Haradzieniec

Reputation: 9340

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

Upvotes: 2

Views: 8131

Answers (3)

Shai
Shai

Reputation: 7317

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.

Upvotes: 5

Jack
Jack

Reputation: 2830

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

Upvotes: 3

catfood
catfood

Reputation: 4331

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

Upvotes: 2

Related Questions