第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

MySQL 別名使用

標(biāo)簽:
MySQL

Summary: in this tutorial, you will learn how to use MySQL alias to improve the readability of the queries.

MySQL supports two kinds of aliases which are known as column alias and table alias. Let’s examine each kind of alias in detail.

MySQL alias for columns

Sometimes the names of columns are so technical that make the query’s output difficult to understand. To give a column a descriptive name, you use a column alias. The following illustrates how to use the column alias:

SELECT [col1 | expression] AS `descriptive name` FROM table_name

To give a column an alias, you use the AS keyword followed by the alias. If the alias contains space, you must quote the it as shown in the syntax. Because the AS keyword is optional, you can omit it in the statement.

Note that you can also give an expression an alias.

Let’s look at the employees table in the sample database.

Employees Table

The following query selects first names and last names of employees, and combine them to produce the full names. The CONCAT_WS function is used to concatenate first name and last name.

SELECT CONCAT_WS(', ',lastName, firstname) FROM employees;

Without MySQL Alias

The column heading is quite difficult to read. You can assign the heading of the output a column alias to make it more readable as the following query:

SELECT CONCAT_WS(', ',lastName, firstname) AS `Full name` FROM employees;

MySQL Alias for column

In MySQL, you can use the column alias in the ORDER BYGROUP BY and HAVING clauses to refer to the column.

The following query uses the column alias in the ORDER BY clause to sort the employee’s full names alphabetically:

SELECT CONCAT_WS(', ',lastName, firstname) `Full name` FROM employees ORDER BY `Full name`;

MySQL Alias in ORDER BY

The following statement selects the order whose total amount is greater than 60000. It uses column aliases in GROUP BY and HAVING clauses.

SELECT orderNumber `Order no.`, SUM(priceEach * quantityOrdered) Total FROM orderDetails GROUP BY `Order no.` HAVING total > 60000;

MySQL Alias in GROUP BY and HAVING clauses

Notice that you cannot use column alias in the WHERE clause. The reason is that when MySQL evaluates the WHEREclause, the values of columns specified in the SELECT clause may not be determined yet.

MySQL alias for tables

An alias also gives a table a different name. You assign a table an alias by using the AS keyword as the following syntax:

table_name AS table_alias

The alias for the table is called table alias. Like the column alias, the AS keyword is optional so you can omit it.

You often use the table alias in the statement that contains INNER JOINLEFT JOINself join clauses, and in subqueries.

Let’s look at the customers and orders tables:

Customers and Orders Tables

See the following query:

SELECT customerName, COUNT(o.orderNumber) total FROM customers c INNER JOIN orders o ON c.customerNumber = o.customerNumber GROUP BY customerName ORDER BY total DESC

MySQL table alias

The query above selects customer name and the number of orders from the customers and orders tables. It uses c as a table alias for the customers table and o as a table alias for the orders table. The columns in the customersand orders tables is referred via the table aliases.

If you do not use alias in the query above, you have to use the table name to refer to its columns, which makes the query lengthy and less readable.

In this tutorial, we have shown you how to use MySQL alias to make your query easy to read and simple to understand.

原文链接:http://outofmemory.cn/mysql/mysql-alias

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶(hù)
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消