Keyword Order By sa PHP MySQL

Ang keyword na ORDER BY ay ginagamit upang ayusin ang datos sa record set.

Keyword na ORDER BY

Ang keyword na ORDER BY ay ginagamit upang ayusin ang datos sa record set.

Grammar

SELECT column_name(s)
FROM table_name
ORDER BY column_name

Komentaryo:Ang SQL ay hindi nakakahiwalay sa pag-iral ng malaki at maliit na titik. ORDER BY ay katumbas ng order by.

Halimbawa

Ang halimbawa na ito ay pinili ang lahat ng datos na naka-imbak sa "Persons" table at naihanda ang resulta ayon sa "Age" column:

<?php
$_con = mysql_connect("localhost","peter","abc123");
kapag (!$_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 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 second column is used when the first column is the same:

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