lalith222
lalith222

Reputation: 269

How to get the data by particular order from mysql database

i have one mysql database table with a column like favorite having values 1,0,0,1,0,0,1. how to get the table data with favorite =1 first after that favorite=0, please help me to solve this problem

Upvotes: 1

Views: 562

Answers (5)

Hemang
Hemang

Reputation: 1234

Use this code.

SELECT * FROM TableName ORDER BY ColumnName ASC/DESC

You can refer this link. It'll be very useful.

http://thetricky.net/mySQL/GROUP%20BY%20vs%20ORDER%20BY

Thanks, Hemang.

Upvotes: 1

Omesh
Omesh

Reputation: 29081

IF you have only two values 0 and 1 then you can use union all query as well

SELECT * FROM tbl_name WHERE favorite = 1
UNION ALL
SELECT * FROM tbl_name WHERE favorite = 0;

Upvotes: 0

000
000

Reputation: 3950

SELECT * FROM `<tablename>` order by favorite DESC;

Upvotes: 0

Random Guy
Random Guy

Reputation: 2900

Try this query :

SELECT favorite FROM tbl_name ORDER BY favorite DESC;

Upvotes: 3

Adriaan Stander
Adriaan Stander

Reputation: 166386

You need to use ORDER BY favorite DESC in the SELECT statement

Have a look at 3.3.4.4. Sorting Rows

Upvotes: 3

Related Questions