Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put row on top in mysql query.

Tags:

php

mysql

Hi i have 100 records in my SQL table i want to sort them ASC by name but i need one record on top of all record nr 43.

Is there way i can pull this record 43 first and then everything else ASC order by name?

Trick is to do it in one query.

like image 991
BUddhaxx Avatar asked Jul 29 '11 12:07

BUddhaxx


People also ask

What is the MySQL query to display the top 20 rows?

The SELECT TOP statement in SQL shows the limited number of records or rows from the database table. The TOP clause in the statement specifies how many rows are returned. It shows the top N number of rows from the tables in the output.

Can I use top in MySQL?

Note − All the databases do not support the TOP clause. For example MySQL supports the LIMIT clause to fetch limited number of records while Oracle uses the ROWNUM command to fetch a limited number of records.

How do I get top 10 in MySQL?

To select first 10 elements from a database using SQL ORDER BY clause with LIMIT 10. Insert some records in the table using insert command. Display all records from the table using select statement. Here is the alternate query to select first 10 elements.

What is top command in MySQL?

The SQL TOP clause is used to limit the number of rows returned. Its basic syntax is: SELECT TOP number | percent column_list FROM table_name ; Here, column_list is a comma separated list of column or field names of a database table (e.g. name, age, country, etc.)


1 Answers

No UNIONs or CASEs needed:

ORDER BY id = 43 DESC, name ASC
like image 129
sqwk Avatar answered Sep 28 '22 07:09

sqwk