显示一个主要项目,然后按字母顺序跟随其他项目

Is there a MYSQL or PHP method to display an item first before all others then display the rest of my items alphabetically?

SELECT client_name FROM clients ORDER BY client_name ASC

Gives me the following list:

  1. Bob
  2. Harold
  3. Sandy
  4. Timmy

Except I am looking for a way to display them in order like this.

  1. Harold
  2. Bob
  3. Sandy
  4. Timmy

or like this depending on which one I need first ...

  1. Sandy
  2. Bob
  3. Harold
  4. Timmy

You can use something like this:

SELECT client_name FROM clients ORDER BY client_name != 'Harold', client_name

The client_name != 'Harold' part is false for 'Harold' and true for every other row, so 'Harold' will be first. After that, you sort the rows where it's true by client_name.

If you wanted to do this in MySQL, you could use an union:

SELECT client_name FROM clients WHERE client_name = 'Harold'
UNION SELECT client_name FROM clients WHERE client_name != 'Harold' ORDER BY client_name ASC

I'd rather use Lukáš Lalinský's suggestion though, that was great.

Or, just filter it out in PHP:

$result = mysql_query("SELECT client_name FROM clients ORDER BY client_name ASC");
$head = array();
$tail = array();

while($row = mysql_fetch_assoc($result)) {
    if($row['client_name'] == 'Harold') {
        $head[] = $row;
    } else {
        $tail[] = $row;
    }
}

$result = array_merge($head, $tail);
SELECT client_name FROM clients ORDER BY client_name ASC LIMIT 1
you will get $clientname
then:
SELECT client_name FROM clients ORDER BY client_name ASC LIMIT 1,99999

OR:

SELECT client_name FROM clients WHERE client_name<>$clientname ORDER BY client_name ASC