Home How do I return rows with a specific value first?
Reply: 0

How do I return rows with a specific value first?

user7421 Published in September 19, 2018, 11:45 am

I want my query to return the rows of the table where a column contains a specific value first, and then return the rest of the rows alphabetized.

If I have a table something like this example:

 - Table: Users
 - id - name -  city
 - 1    George  Seattle
 - 2    Sam     Miami
 - 3    John    New York
 - 4    Amy     New York
 - 5    Eric    Chicago
 - 6    Nick    New York

And using that table I want to my query to return the rows which contain New York first, and then the rest of the rows alphabetized by city. Is this possible to do using only one query?

share|improve this question
  • Consider changing the accepted answer to the second one if you can, as the first one only works for MySQL and not for MSSQL. – Magisch Mar 6 '17 at 8:23
  • Done. My original question was about MySQL, but the tags did not reflect that anyway. – Phoexo Mar 6 '17 at 15:25
  • Thank you. I came across this this morning in an effort to do something similar for a problem I had. :) – Magisch Mar 6 '17 at 16:29

3 Answers 3

active oldest votes
up vote 170 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.367653 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO