Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL AS keyword [duplicate]

Tags:

mysql

I'm not professional in query writing, but wrote many from the time I began MySQL. Recently I noticed there is no need to type AS keyword in name aliasing.

SELECT name AS n

equals to

SELECT name n

However I know that this ability is out from years ago. I've 2 questions around this subject:

  1. Is AS keyword redundant?
  2. Sometimes ago when I encountered with a custom query on a website with having no AS in aliasing, that its executing made MySQL service down, I changed the way of name aliasing with adding AS keyword and this little change made it work!

    What was the problem here?

like image 275
revo Avatar asked Oct 24 '13 13:10

revo


People also ask

What is duplicate key in MySQL?

ON DUPLICATE KEY UPDATE is a MariaDB/MySQL extension to the INSERT statement that, if it finds a duplicate unique or primary key, will instead perform an UPDATE. The row/s affected value is reported as 1 if a row is inserted, and 2 if a row is updated, unless the API's CLIENT_FOUND_ROWS flag is set.

Can primary key be duplicate in MySQL?

Since both primary key and unique columns do not accept duplicate values, they can be used for uniquely identifying a record in the table. This means that, for each value in the primary or unique key column, only one record will be returned.

How do I find duplicate keys in MySQL?

First, use the GROUP BY clause to group all rows by the target column, which is the column that you want to check duplicate. Then, use the COUNT() function in the HAVING clause to check if any group have more than 1 element. These groups are duplicate.

How do I prevent duplicates in MySQL?

Note − Use the INSERT IGNORE command rather than the INSERT command. If a record doesn't duplicate an existing record, then MySQL inserts it as usual. If the record is a duplicate, then the IGNORE keyword tells MySQL to discard it silently without generating an error.


1 Answers

For the answer to 1), the mysql documentation http://dev.mysql.com/doc/refman/5.0/en/select.html:

A select_expr can be given an alias using AS alias_name. The alias is used as the expression's column name and can be used in GROUP BY, ORDER BY, or HAVING clauses. For example:

SELECT CONCAT(last_name,', ',first_name) AS full_name   FROM mytable
ORDER BY full_name;

The AS keyword is optional when aliasing a select_expr with an identifier. The preceding example could have been written like this:

SELECT CONCAT(last_name,', ',first_name) full_name   FROM mytable
ORDER BY full_name;

However, because the AS is optional, a subtle problem can occur if you forget the comma between two select_expr expressions: MySQL interprets the second as an alias name. For example, in the following statement, columnb is treated as an alias name:

SELECT columna columnb FROM mytable;

For this reason, it is good practice to be in the habit of using AS explicitly when specifying column aliases.

It is not permissible to refer to a column alias in a WHERE clause, because the column value might not yet be determined when the WHERE clause is executed. See Section C.5.5.4, “Problems with Column Aliases”.

This also very similar syntactically for tables:

A table reference can be aliased using tbl_name AS alias_name or tbl_name alias_name:

SELECT t1.name, t2.salary FROM employee AS t1, info AS t2   WHERE
t1.name = t2.name;

SELECT t1.name, t2.salary FROM employee t1, info t2   WHERE t1.name =
t2.name;

For the Answer to 2) I don't think there is anyway you can tell without a full description of the software versions, query being run and error messages received.

like image 184
ModulusJoe Avatar answered Sep 24 '22 06:09

ModulusJoe