MySQL Order By语法介绍_MySQL
bitsCN.com
今天在使用ORDER BY的过程中出现了一点问题,发现之前对ORDER BY理解是错误的。
之前在w3s网站上看到ORDER BY的用法,以为是对选出来的数据按关键字升序或者降序排列,结果今天尝试select数据集数据的时候,发现使用ORDER BY 和ORDER BY DESC得出的查询结果完全不一样,按照自己之前的理解它们应该是结果相同,而内部顺序不一样而已。
问了一下同事,查了一下文档,才恍然大悟。如果我们在执行select语句的时候使用ORDER BY (DESC),那么它首先会对所有记录按照关键字有一个排序,然后依次读取所需的记录,而不是先选出记录再进行降序排列。 一个概念性的错误,所以记下来警示自己。
MySQL Order By keyword是用来给记录中的数据进行分类的。
MySQL Order By Keyword根据关键词分类
ORDER BY keyword是用来给记录中的数据进行分类的。
MySQL Order By语法
SELECT column_name(s)
FROM table_name
ORDER BY column_name
注意:SQL语句是“字母大小写不敏感”的语句(它不区分字母的大小写),即:“ORDER BY”和“order by”是一样的。
MySQL Order By案例
下面的例子:从“Person”表中选取所有记录,并将“Age”列进行分类:
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM person ORDER BY age");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName']
echo " " . $row['LastName'];
echo " " . $row['Age'];
echo "
";
}
mysql_close($con);
?>
[html]
上面的代码将输出下面的结果:
Glenn Quagmire 33
Peter Griffin 35
按照升序或者降序进行分类排列
如果你使用了“ORDER BY”关键词,所有记录将按照默认的升序进行排列(即:从1到9,从a到z)
使用“DESC”关键词可以制定所有的数据按照降序排列(即:从9到1,从z到a):
[code]
SELECT column_name(s)
FROM table_name
ORDER BY column_name DESC
MySQL Order By根据两列进行分类
很多时候,我们需要同时根据两列内容(或者更多列)来对数据进行分类。当指定的列数多于一列时,仅在第一列的值完全相同时才参考第二列:
SELECT column_name(s)
FROM table_name
ORDER BY column_name1, column_name2
bitsCN.com

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Websites for learning C language: 1. C Language Chinese Website; 2. Rookie Tutorial; 3. C Language Forum; 4. C Language Empire; 5. Script House; 6. Tianji.com; 7. Red and Black Alliance; 8, 51 Self-study network; 9. Likou; 10. C Programming. Detailed introduction: 1. C language Chinese website, which is a website dedicated to providing C language learning materials for beginners. It is rich in content, including basic grammar, pointers, arrays, functions, structures and other modules; 2. Rookie tutorials, This is a comprehensive programming learning website and more.

Do you frequently visit the same website at about the same time every day? This can lead to spending a lot of time with multiple browser tabs open and cluttering the browser while performing daily tasks. Well, how about opening it without having to launch the browser manually? It's very simple and doesn't require you to download any third-party apps, as shown below. How do I set up Task Scheduler to open a website? Press the key, type Task Scheduler in the search box, and then click Open. Windows On the right sidebar, click on the Create Basic Task option. In the Name field, enter the name of the website you want to open and click Next. Next, under Triggers, click Time Frequency and click Next. Select how long you want the event to repeat and click Next. Select enable

In macOS Sonoma and Safari 17, you can turn websites into "web apps," which can sit in your Mac's dock and be accessed like any other app without opening a browser. Read on to learn how it works. Thanks to a new option in Apple's Safari browser, it's now possible to turn any website on the internet you frequently visit into a standalone "web app" that lives in your Mac's dock and is ready for you to access at any time. The web app works with Mission Control and Stage Manager like any app, and can also be opened via Launchpad or SpotlightSearch. How to turn any website into

Methods to check dead links on a website include using online link tools, using webmaster tools, using robots.txt files, and using browser developer tools. Detailed introduction: 1. Use online link tools. There are many online dead link detection tools, such as LinkDeath, LinkDefender and Xenu. These tools can automatically detect dead links in the website; 2. Use webmaster tools. Most webmasters Tools, such as Google's Webmaster Tools, Baidu's Webmaster Tools, etc., all provide dead link detection functions and so on.

To solve the problem of Python website access speed, use database optimization methods such as indexing and caching. In the process of developing and maintaining Python websites, we often encounter the problem of slow website access speed. In order to improve the response speed of the website, we can use some database optimization methods, such as indexing and caching. This article will introduce how to use these methods to solve Python website access speed problems, and provide corresponding code examples for reference. 1. Use indexes to optimize database queries. Indexes are a fast search structure for data in the database, which can greatly

PHP website: 1. Facebook, one of the largest social media platforms in the world; 2. WordPress, an open source content management system for quickly creating and managing various types of websites; 3. Magento, a powerful e-commerce platform , used to create and manage online stores; 4. Joomla, a popular open source content management system used to build various types of websites; 5. Wikipedia, a free online encyclopedia that provides knowledge and information on various topics; 6. Digg, social news sites and more.

In-depth analysis of the role and usage of the static keyword in C language. In C language, static is a very important keyword, which can be used in the definition of functions, variables and data types. Using the static keyword can change the link attributes, scope and life cycle of the object. Let’s analyze the role and usage of the static keyword in C language in detail. Static variables and functions: Variables defined using the static keyword inside a function are called static variables, which have a global life cycle

Title: Is go a keyword in C language? Detailed analysis In C language, "go" is not a keyword. Keywords in C language are specified by the C standard and are used to represent specific grammatical structures or functions. They have special meanings in the compiler and cannot be used as identifiers or variable names. For example, the keyword "int" represents an integer data type, "if" represents a conditional statement, and so on. If we want to verify whether "go" is a keyword in C language, we can write a simple program to test it. Here is an example: #inc
