Beginners guide to MySQL and MariaDB
Chapters
Sorting Results
The ORDER BY clause on column or columns in MySQL allows the sorting of result set in Ascending and Descending order.
Syntax:
SELECT field1, field2,...fieldN table_name1, table_name2...
ORDER BY field1, [field2...] [ASC [DESC]]
ORDER BY Example:
Here is an example showing sorting the Books by Author name in Ascending and Descending order
In our PHP script we can use the exact same queries as shown above
Descending order
<?php $server = "localhost"; $user = "username"; $pwd = "password"; try { $connection = new PDO("mysql:host=$server;dbname=testDB", $user, $pwd); $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "select * from book_tbl ORDER BY book_author DESC;"; $select = $sql->prepare($sql); $select->execute(); $result = $select->fetchAll(); var_dump($result); } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); }
?>
Ascending order
<?php $server = "localhost"; $user = "username"; $pwd = "password"; try { $connection = new PDO("mysql:host=$server;dbname=testDB", $user, $pwd); $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// even if you don't specify ASC keyword in the query below, by default the sort or is ASC $sql = "select * from book_tbl ORDER BY book_author ASC;"; $select = $sql->prepare($sql); $select->execute(); $result = $select->fetchAll(); var_dump($result); } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); }
?>
In our next part of this tutorial series, we will take a look on JOINS
Description
In this tutorial, we will cover few topics that will give you a heads on start to build your knowledge on. Topics that we will cover briefly but still providing enough information are listed below
- Overview
- Installing on Linux and Windows
- Some useful admin queries for starters
- Connection
- Create Database
- Drop Database
- Select Database
- Data Type
- Create Table
- Drop Table
- Inserting and Selecting data
- Where Clause
- Updating and deleting data
- Like Clause
- Sorting Result
- Using Joins
- Brief introduction to Regex, Transactions and Indexes
- Alter Command
- Temporary Tables
- Database Info
- Using Sequence
- Database Export and Import
- Resetting MySQL/MariaDB Administrator password
Audience
Absolute beginners looking to get a sneak peak into what MySQL. Please remember that this is not a full on guide but a quick introduction to the subject.
Learning Objectives
Get to know MySQL and MariaDB
Author: Subject Coach
Added on: 23rd Jun 2015
You must be logged in as Student to ask a Question.
None just yet!