1. Úvod
The MySQL AS keyword is used to assign aliases (alternative names) to tables and columns within a query.
This feature not only improves the readability of SQL statements but also helps express complex queries more concisely.
In this article, we will explain everything from the basic usage of AS to practical examples and important considerations.
To ensure beginners can easily understand the concept, we will include clear and concrete code examples throughout.
2. Základní použití klíčového slova „AS“
2.1. Přiřazení aliasu ke sloupci
In MySQL, you can assign an alias to a column to use a more descriptive or readable column name.
The basic syntax is as follows:
SELECT column_name AS alias_name FROM table_name;
Příklad
For example, if you want to concatenate first_name and last_name from the users table and assign the alias full_name:
SELECT first_name, last_name, CONCAT(first_name, ' ', last_name) AS full_name FROM users;
When you execute this SQL statement, a column named full_name will be created, displaying the combined values of first_name and last_name.
2.2. Přiřazení aliasu k tabulce
You can also assign aliases to tables.
This allows you to shorten long table names and improve the readability of complex queries.
The basic syntax is as follows:
SELECT t1.column_name FROM table_name AS t1;
Příklad
For example, to assign the alias e to the employees table:
SELECT e.id, e.name FROM employees AS e;
By using this approach, you can write SQL statements more concisely by using e instead of employees. 
3. Proč používat klíčové slovo „AS“?
3.1. Zlepšení čitelnosti dotazu
Using AS makes queries easier to understand intuitively.
It is especially useful when column names are long or when handling calculated results as columns.
3.2. Předejití nejednoznačnosti při spojování více tabulek
When using JOIN, if the same column name exists in multiple tables, ambiguity can occur.
In such cases, assigning aliases to tables helps prevent confusion.
SELECT e.name, d.department_name FROM employees AS e JOIN departments AS d ON e.department_id = d.id;
In this query, the employees table is shortened to e, and the departments table is shortened to d, making the query more readable.
3.3. Přiřazení aliasů vypočítaným sloupcům nebo výsledkům funkcí
By assigning an alias to calculation results or function outputs in SQL, you can produce clearer and more understandable output.
SELECT price * quantity AS total_cost FROM orders;
In this query, the result of price * quantity is given the descriptive name total_cost.
4. Praktické příklady
4.1. Použití „AS“ při spojování více tabulek
When joining different tables, assigning aliases helps shorten and simplify the query.
SELECT u.id, u.name, o.order_date
FROM users AS u
JOIN orders AS o ON u.id = o.user_id;
In this example, the users table is defined as u, and the orders table is defined as o, eliminating redundant descriptions.
4.2. Přiřazení aliasu k vypočítanému výsledku
When calculating a total amount, assigning an alias makes the output easier to understand.
SELECT product_name, price * quantity AS total_price FROM sales;
As a result, the total_price column contains the calculated value of price * quantity.
5. Důležité poznámky
5.1. „AS“ je volitelné, ale doporučuje se explicitní použití
In MySQL, AS can be omitted. However, for better readability and maintainability, it is recommended to write it explicitly.
SELECT first_name full_name FROM users; -- AS can be omitted
SELECT first_name AS full_name FROM users; -- Recommended
5.2. Použití vyhrazených klíčových slov jako aliasů
If you use an SQL reserved keyword as an alias, you must enclose it in backticks.
SELECT column_name AS `key` FROM table_name;
6. ČPP (Často kladené otázky)
Otázka 1: Je v pořádku vynechat „AS“?
Odpověď 1: Ano, lze to vynechat. Nicméně pro lepší čitelnost se doporučuje použít ji explicitně.
Otázka 2: Mohu přiřadit stejný alias jak tabulce, tak sloupci?
Odpověď 2: Aby se předešlo zmatku, je nejlepší používat různé aliasy pro tabulky a sloupce.
Otázka 3: Může alias obsahovat mezery?
Odpověď 3: Ano, je to možné, ale musíte ho uzavřít do dvojitých uvozovek "" nebo backticků.
SELECT column_name AS "My Custom Name" FROM table_name;
Otázka 4: Mohu použít „AS“ k vytvoření dočasné tabulky?
Odpověď 4: Klíčové slovo AS samo o sobě se nepoužívá samostatně k vytvoření dočasné tabulky.
K vytvoření dočasné tabulky použijte CREATE TEMPORARY TABLE.
CREATE TEMPORARY TABLE temp_table AS SELECT * FROM original_table;
Otázka 5: Mohu použít „AS“ k vytvoření zobrazení?
Odpověď 5: Ano. Při vytváření zobrazení můžete použít AS k definování dotazu zobrazení.
CREATE VIEW sales_summary AS
SELECT product_name, SUM(price * quantity) AS total_sales FROM sales GROUP BY product_name;
7. Shrnutí
V tomto článku jsme podrobně vysvětlili klíčové slovo MySQL AS.
Klíčové body jsou následující:
- Použitím
ASmůžete přiřadit jasné a popisné aliasy sloupcům a tabulkám. - Zlepšuje čitelnost dotazů a usnadňuje porozumění SQL.
- Při používání rezervovaných klíčových slov jako aliasů je musíte uzavřít do backticků.
Klíčové slovo AS je velmi užitečnou funkcí při práci s MySQL.
Používejte ho efektivně k psaní čistých, efektivních a čitelných SQL dotazů!


