Jump to content

Recommended Posts

I havn't had a question yet, although I can't stop trolling this forum. So instead, I have a time saving tip for all of you that insist on using [b]AS[/b] in your queries.
You really don't need it.

[code]
select user_name as name from users;
#would be the same as
select user_name name from users;
[/code]

You can rename you tables the same way, but doing so only helps if you're using multiple tables
[code]
select
  u.user_name
,u.street_address
,s.score
from
  users u
  ,really_dumb_and_long_table_name s
[/code]

If you rename fields and tables, [b]WHERE[/b] statements will accept your renamed table, but [b]NOT [/b] your renamed field. Renaming fields only effects the output.

[code]
select
  u.userID id
,u.user_name name
,u.street_address addy
,s.score
from
  users u
  ,really_dumb_and_long_table_name s
where 1
and u.userID = s.userID

# u.id = s.userID  will not work  (unknown column 'u.id')
[/code]

Renaming tables is also helpful if you ever want to join a table to itself, but identify them as separate objects. But most of the time I get really sick of retyping the whole table name again in large queries (hence the single letter aliases).

Link to comment
https://forums.phpfreaks.com/topic/35328-tip-as-is-superfluous/
Share on other sites

4. Change the table structure ever and don't want to rewrite every query.
5. Want to parse to have less work to do with really long table/field names.
6. Use expressions, and want to be able to have a meaningful, usable hash key name in the output.
Link to comment
https://forums.phpfreaks.com/topic/35328-tip-as-is-superfluous/#findComment-167463
Share on other sites

  • 4 weeks later...
I think he's saying the "AS" keyword is superfluous not actually aliasing tables or fields.

But I would argue against that
1) For improved readability. Having an AS there makes it easier to understand what you were doing.
2) Its easier to spot errors where you simply forget to include a , as that will alias the first column as the second
Link to comment
https://forums.phpfreaks.com/topic/35328-tip-as-is-superfluous/#findComment-186614
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.