عبارت Order By در PHP MySQL

کلمه کلیدی ORDER BY برای مرتب کردن داده‌های درون مجموعه‌ی ثبت شده استفاده می‌شود.

کلمه کلیدی ORDER BY

کلمه کلیدی ORDER BY برای مرتب کردن داده‌های درون مجموعه‌ی ثبت شده استفاده می‌شود.

نحوه‌ی نوشتن

SELECT column_name(s)
FROM table_name
ORDER BY column_name

توضیح:SQL نسبت به حروف بزرگ و کوچک بی‌تفاوت است. ORDER BY با order by یکسان است.

مثال

مثال زیر از تمام داده‌های ذخیره شده در جدول "Persons" استفاده می‌کند و نتایج را بر اساس ستون "Age" مرتب می‌کند:

<?php
$con = mysql_connect("localhost","peter","abc123");
اگر (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons ORDER BY age");
while($row = mysql_fetch_array($result))
  {
  echo $row['FirstName'];
  echo " " . $row['LastName'];
  echo " " . $row['Age'];
  echo "<br />";
  }
mysql_close($con);
?>

The output of the above code:

Glenn Quagmire 33
Peter Griffin 35

Ascending or descending order sorting

If you use the ORDER BY keyword, the default sorting order of the record set is ascending (1 before 9, "a" before "p"):

Please use the DESC keyword to set descending order (9 before 1, "p" before "a"):

SELECT column_name(s)
FROM table_name
ORDER BY column_name DESC

Sort by two columns

You can sort by multiple columns. When sorting by multiple columns, only the first column is used when the first column is the same:

SELECT column_name(s)
FROM table_name
ORDER BY column_name1, column_name2