One: MySQL Alias
1. Introduction
Use MySQL aliases to improve the readability of your queries.
MySQL supports two aliases, called Column aliases and table aliases.
Sometimes the names of the columns are expressions that make the output of the query difficult to understand. To give a descriptive name to a column, you can use a column alias.
Usage:
SELECT [column_1 | expression] AS descriptive_name FROM table_name;
To add an alias to a column, you can use a AS keyword followed by an alias. If the alias contains spaces, you must refer to the following:
SELECT [column_1 | expression] AS `descriptive name` FROM table_name;
Because the AS keyword is optional, you can omit it from the statement.
2. Column Aliases
The query selects the employee's first and last name and combines it to produce the full name. CONCAT_WSThe function is used to concatenate first and last names.
SELECT CONCAT_WS(‘, ‘, lastName, firstname) AS `Full name`FROM employees;
3. Use of clauses for aliases
In MySQL, you can use the column alias in the Order By,group by and the HAVING clause to refer to the column.
The following query uses the ORDER BY column aliases in the clauses to arrange the full name of the employee alphabetically:
SELECT CONCAT_WS(‘ ‘, lastName, firstname) `Full name`FROM employeesORDER BY `Full name`;
The following statement queries 60000 for orders that have a total amount greater than. It GROUP BY HAVING uses column aliases in the and clauses.
select ordernumber ' Order no. ' sum* quantityordered) Total From Orderdetailsgroup by ' order no. ' having total > 60000
4. Attention Points
Note that column aliases cannot be used in the WHERE clause. The reason is that the WHERE SELECT value of the column specified in the clause may not be determined when MySQL evaluates the evaluation clause.
5. Aliases for tables
You can use aliases to add different names to the table. Use AS the keyword to assign an alias in the table name, following the query statement syntax:
table_name AS table_alias
Two tables have the same column name: customerNumber . If you do not use a table alias to specify which table is a customerNumber column in:
SELECT CustomerNamecount (O.ordernumberfrom Customers cinner join orders o span class= "token keyword" >on c.customernumber = o.customernumbergroup by customernamehaving total >=< Span class= "token number" >5order by total desc
Two: INNER join inside joins
1.
Three:
Four:
MySQL Connection table---to be added