I’m fairly new to PHP/MySql and using queries in general. I was just wondering if there’s any benefit to using “AS” in a query other than trying to make it look cleaner? Does it speed up the query at all? I probably could have figured this out by a google search but I wanted to ask my first question and see how this works. I WILL select an answer (unlike some people…)
with:
SELECT
news.id as id
news.name as name
FROM news
without:
SELECT
news.id
news.name
FROM news
A more complex example from a many-to-many relationship tutorial I found:
SELECT
c.name,
cf.title
FROM celebrities AS c
JOIN (
SELECT
icf.c_id,
icf.f_id,
f.title
FROM int_cf AS icf
JOIN films AS f
ON icf.f_id = f.f_id
) AS cf
ON c.c_id = cf.c_id
ORDER BY c.c_id ASC
There’s no reason to use it if you know there will be no conflicts with other columns. There are no differences in performance, but it does change the name of the output column. It’s really useful for when you construct dynamic selects. So for instance, if you had a
first_nameand alast_namefield, you could use the CONCAT function to do:Furthermore, the AS operator works when defining sub-query tables, as you showed in that JOIN. Without that AS, you wouldn’t be able to reference that table in the ON clause or the SELECT fields.