Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort by last name and first name

Tags:

sql

mysql

I have a small little problem. I am have a drop down list that displays all of our customers names. As of right now, the list is sorted by last name (A-Z). But for example, there are 250 people with the last name "Smith". What I need is a way that the last name of each customer to stay the way it is but I need the first names to be listed alphabetically as well. Here is an example:

Customer #1 - Smith, Jean
Customer #2 - Smith, Allen
Customer #3 - Smith, Davey

What I would like is this:

Customer #1 - Smith, Allen
Customer #2 - Smith, Davey
Customer #3 - Smith, Jean

Here is how I am pulling the list now:

SELECT customer_id, first_name, last_name FROM customers ORDER BY last_name
like image 376
three3 Avatar asked Oct 29 '25 08:10

three3


1 Answers

Adjust your SQL statement, add the ORDER BY clause with the appropriate field names:

ORDER BY last_name ASC, first_name ASC

like image 123
Josh Avatar answered Oct 31 '25 01:10

Josh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!