MySQL Order By

6 min read

Select and Order Data From a MySQL Database #

MySQL мэдээллийн сангаас өгөгдөл сонгох, захиалах #

The ORDER BY clause is used to sort the result-set in ascending or descending order.

ORDER BY гэсэн өгүүлбэр нь үр дүнгийн багцыг өсөх, буурах дарааллаар эрэмбэлэхэд хэрэглэгддэг.

The ORDER BY clause sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

ORDER BY гэсэн өгүүлбэр нь бүртгэлийг анхдагчаар өсөх дарааллаар эрэмбэлдэг. Бичлэгийг буурах дарааллаар эрэмбэлэхийн тулд DESC түлхүүр үгийг ашиглана уу.

SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC 

To learn more about SQL, please visit our SQL tutorial.

SQL-ийн талаар илүү ихийг мэдэхийг хүсвэл манай SQL заавраар зочилно уу.


Select and Order Data With MySQLi #

MySQLi ашиглан өгөгдлийг сонгож захиалах #

The following example selects the id, firstname and lastname columns from the MyGuests table. The records will be ordered by the lastname column:

Дараах жишээнд MyGuests хүснэгтээс id, namename, lastname багануудыг сонгоно. Бичлэгийг овог нэрийн баганаар захиалах болно:

Example Жишээ #

(MySQLi Object-oriented) #

<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT id, firstname, lastname FROM MyGuests ORDER BY lastname"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; } } else { echo "0 results"; } $conn->close(); ?>

Code lines to explain from the example above:

Дээрх жишээнээс тайлбарлах кодын мөрүүд:

First, we set up the SQL query that selects the id, firstname and lastname columns from the MyGuests table. The records will be ordered by the lastname column. The next line of code runs the query and puts the resulting data into a variable called $result.

Нэгдүгээрт, MyGuests хүснэгтээс id, name, lastname баганыг сонгох SQL асуулга тохируулав. Бичлэгүүдийг овог нэрийн баганаар захиалах болно. Дараагийн код мөр нь асуулга ажиллуулж, өгөгдлийг $ result нэртэй хувьсагч руу оруулна.

Then, the function num_rows() checks if there are more than zero rows returned.

Дараа нь function num_rows() буцааж тэгээс их мөр байгаа эсэхийг шалгана.

If there are more than zero rows returned, the function fetch_assoc() puts all the results into an associative array that we can loop through. The while() loop loops through the result set and outputs the data from the id, firstname and lastname columns.

Хэрэв тэгээс дээш мөрийг буцааж өгвөл fetch_assoc() функц нь бүх үр дүнг ассоциатив багцад оруулдаг. while() давталт нь үр дүнгийн багцыг тойрон эргэлдэж id, namename, namen багануудаас өгөгдлийг гаргана.

The following example shows the same as the example above, in the MySQLi procedural way:

Дараах жишээг дээрх жишээний нэгэн адил MySQLi процедурын аргаар харуулав:

Example Жишээ #

(MySQLi Procedural) #

<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "SELECT id, firstname, lastname FROM MyGuests ORDER BY lastname"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; } } else { echo "0 results"; } mysqli_close($conn); ?>

You can also put the result in an HTML table:

Та үр дүнг HTML хүснэгтэд оруулж болно:

Example Жишээ #

(MySQLi Object-oriented) #

<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT id, firstname, lastname FROM MyGuests ORDER BY lastname"; $result = $conn->query($sql); if ($result->num_rows > 0) { echo "<table><tr><th>ID</th><th>Name</th></tr>"; // output data of each row while($row = $result->fetch_assoc()) { echo "<tr><td>".$row["id"]."</td><td>".$row["firstname"]." ".$row["lastname"]."</td></tr>"; } echo "</table>"; } else { echo "0 results"; } $conn->close(); ?>

Select Data With PDO (+ Prepared Statements) #

PDO ашиглан өгөгдлийг сонгох (+ Бэлтгэсэн мэдэгдэл) #

The following example uses prepared statements.

Дараах жишээнд бэлтгэсэн мэдэгдлийг ашиглав.

Here we select the id, firstname and lastname columns from the MyGuests table. The records will be ordered by the lastname column, and it will be displayed in an HTML table:

Энд бид MyGuests хүснэгтээс id, namename, lastname багануудыг сонгоно. Бичлэгүүдийг овог нэрийн баганаар захиалах бөгөөд үүнийг HTML хүснэгтэд харуулах болно.

Example Жишээ (PDO) #

<?php echo "<table style='border: solid 1px black;'>"; echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>"; class TableRows extends RecursiveIteratorIterator { function __construct($it) { parent::__construct($it, self::LEAVES_ONLY); } function current() { return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>"; } function beginChildren() { echo "<tr>"; } function endChildren() { echo "</tr>" . "\n"; } } $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDBPDO"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $conn->prepare("SELECT id, firstname, lastname FROM MyGuests ORDER BY lastname"); $stmt->execute(); // set the resulting array to associative $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { echo $v; } } catch(PDOException $e) { echo "Error: " . $e->getMessage(); } $conn = null; echo "</table>"; ?>

Powered by BetterDocs

Leave a Reply