深入SELECT语句的查询功能(一)(1)
本节将讲述SELECT语句的一些高级功能。 列和表的别名 列的别名 精选输出的列可以用列名、列别名或列位置在ORDER BY和GROUP BY子句引用,列位置从1开始。 例如,我们从pet表中检索出宠物和种类,直接引用列名: mysql> SELECT name,species FROM pet ORDER BY
本节将讲述SELECT语句的一些高级功能。
列和表的别名
列的别名
精选输出的列可以用列名、列别名或列位置在ORDER BY和GROUP BY子句引用,列位置从1开始。
例如,我们从pet表中检索出宠物和种类,直接引用列名:
mysql> SELECT name,species FROM pet ORDER BY name, species;
其输出为:
+----------+---------+
| name | species |
+----------+---------+
| Bowser | dog |
| Buffy | dog |
| Chirpy | bird |
| Claws | cat |
| Fang | dog |
| Fluffy | cat |
| Puffball | hamster |
| Slim | snake |
| Whistler | bird |
+----------+---------+
在子句中使用列的位置:
mysql> SELECT name,species FROM pet ORDER BY 1,2;
这条语句的输出与上面并无不同。
最后,你还可以为列命名:
mysql> SELECT name AS n,species AS s FROM pet ORDER BY n,s;
注意返回的结果:
+----------+---------+
| n | s |
+----------+---------+
| Bowser | dog |
| Buffy | dog |
| Chirpy | bird |
| Claws | cat |
| Fang | dog |
| Fluffy | cat |
| Puffball | hamster |
| Slim | snake |
| Whistler | bird |
+----------+---------+
返回的记录顺序并无不同。但是列的名字有了改变,这一点在使用CREATE TABLE…SELECT语句创建表时是有意义的。
例如,我们想从pet表生成包括其中name,owner字段的表,但是想把name和owner字段的名字重新命名为animal和child,一个很笨的方法就是创建表再录入数据,如果使用别名,则仅仅一条SQL语句就可以解决问题,非常简单,我们要使用的语句使CREATE TABLE:
mysql> CREATE TABLE pet1 -> SELECT name AS animal,owner AS child -> FROM pet; Copy after login |
然后,检索生成的表,看看是否打到目的:
mysql> SELECT * FROM pet1;
+----------+--------+
| animal | child |
+----------+--------+
| Fluffy | Harold |
| Claws | Gwen |
| Buffy | Harold |
| Chirpy | Gwen |
| Fang | Benny |
| Bowser | Diane |
| Whistler | Gwen |
| Slim | Benny |
| Puffball | Diane |
+----------+--------+
在子句中使用列的别名
你可以在GROUP BY、ORDER BY或在HAVING部分中使用别名引用列。别名也可以用来为列取一个更好点的名字:
mysql> SELECT species,COUNT(*) AS total FROM pet -> GROUP BY species HAVING total>1; Copy after login |
+---------+-------+
| species | total |
+---------+-------+
| bird | 2 |
| cat | 2 |
| dog | 3 |
+---------+-------+
注意,你的 ANSI SQL 不允许你在一个WHERE子句中引用一个别名。这是因为在WHERE代码被执行时,列值还可能没有终结。例如下列查询是不合法:
SELECT id,COUNT(*) AS total FROM pet WHERE total > 1 GROUP BY species
会有下面的错误:
ERROR 1054: Unknown column 'total' in 'where clause'
WHERE语句被执行以确定哪些行应该包括GROUP BY部分中,而HAVING用来决定应该只用结果集合中的哪些行。
表的别名
别名不仅可以应用于列,也可以引用于表名,具体方法类似于列的别名,这里不再重复。
列的别名经常用于表自身的连接中。你不必有2个不同的表来执行一个联结。如果你想要将一个表的记录与同一个表的其他记录进行比较,联结一个表到自身有时是有用的。例如,为了在你的宠物之中繁殖配偶,你可以用pet联结自身来进行相似种类的雄雌配对:
mysql> SELECT p1.name, p1.sex, p2.name, p2.sex, p1.species -> FROM pet AS p1, pet AS p2 -> WHERE p1.species = p2.species AND p1.sex = "f" AND p2.sex = "m"; Copy after login |
+--------+------+--------+------+---------+
| name | sex | name | sex | species |
+--------+------+--------+------+---------+
| Fluffy | f | Claws | m | cat |
| Buffy | f | Fang | m | dog |
| Buffy | f | Bowser | m | dog |
+--------+------+--------+------+---------+
在这个查询中,我们为表名指定别名以便能引用列并且使得每一个列引用关联于哪个表实例更直观。

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



With the rapid development of the Internet, IP addresses have become an indispensable part of network communications. IP address information is very important in network security monitoring, traffic management, and targeted e-commerce advertising. Therefore, in order to facilitate users to query IP address/domain name information, many websites provide IP address query functions. This article will introduce how to use PHP to implement the IP address query function for readers' reference. 1. What is an IP address? IP address (InternetProtocolAddress) is the network protocol

Asynchronous processing method of SelectChannelsGo concurrent programming using golang Introduction: Concurrent programming is an important area in modern software development, which can effectively improve the performance and responsiveness of applications. In the Go language, concurrent programming can be implemented simply and efficiently using Channels and Select statements. This article will introduce how to use golang for asynchronous processing methods of SelectChannelsGo concurrent programming, and provide specific

How to hide the select element in jquery: 1. hide() method, introduce the jQuery library into the HTML page, you can use different selectors to hide the select element, the ID selector replaces the selectId with the ID of the select element you actually use; 2. css() method, use the ID selector to select the select element that needs to be hidden, use the css() method to set the display attribute to none, and replace selectId with the ID of the select element.

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:

Because select allows developers to wait for multiple file buffers at the same time, it can reduce IO waiting time and improve the IO efficiency of the process. The select() function is an IO multiplexing function that allows the program to monitor multiple file descriptors and wait for one or more of the monitored file descriptors to become "ready"; the so-called "ready" state is Refers to: the file descriptor is no longer blocked and can be used for certain types of IO operations, including readable, writable, and exceptions. select is a computer function located in the header file #include. This function is used to monitor file descriptor changes—reading, writing, or exceptions. 1. Introduction to the select function. The select function is an IO multiplexing function.

A closer look at HTTP status code 100: What does it mean? The HTTP protocol is one of the most commonly used protocols in modern Internet applications. It defines the standard specifications required for communication between browsers and web servers. During the process of HTTP request and response, the server will return various types of status codes to the browser to reflect the processing of the request. Among them, HTTP status code 100 is a special status code used to indicate "continue". HTTP status codes consist of three digits, each status code has a specific meaning

1. Keywords in SQL statements are not case-sensitive. SELECT is equivalent to SELECT, and FROM is equivalent to from. 2. To select all columns from the users table, you can use the symbol * to replace the column name. Syntax--this is a comment--query out [all] data from the [table] specified by FEOM. * means [all columns] SELECT*FROM--query out the specified data from the specified [table] from FROM Data of column name (field) SELECT column name FROM table name instance--Note: Use English commas to separate multiple columns selectusername, passwordfrom

PHP development skills: Implement data table association and query functions In PHP development, it is often necessary to handle database-related operations, including associations and queries between data tables. This article will introduce how to use PHP to implement the correlation and query functions of data tables, and provide specific code examples. 1. The concept of data table association Data table association refers to connecting the records in two or more data tables through certain rules to obtain the data information of the associated table. Common data table association methods include one-to-one association, one-to-many association and many-to-many association. one
