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_name and a last_name field, you could use the CONCAT function to do:
SELECT CONCAT(first_name, ' ', last_name) AS name FROM whatever
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.
1) No, it doesn't affect performance one way or the other
2) As far as readability of the SQL Query, it's just a matter of preference
3) It does affect the readability of the output (it changes the column headers)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With