bibby Posted January 23, 2007 Share Posted January 23, 2007 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 asselect 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.scorefrom 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.scorefrom users u ,really_dumb_and_long_table_name swhere 1and 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). Quote Link to comment Share on other sites More sharing options...
effigy Posted January 23, 2007 Share Posted January 23, 2007 AS is not superfluous when you're:1. Writing stored procedures.2. Using functions.3. Using SQL to feed methods that are looking for certain field names. Quote Link to comment Share on other sites More sharing options...
fenway Posted January 23, 2007 Share Posted January 23, 2007 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. Quote Link to comment Share on other sites More sharing options...
effigy Posted February 15, 2007 Share Posted February 15, 2007 7. Working with self-joins. Quote Link to comment Share on other sites More sharing options...
artacus Posted February 16, 2007 Share Posted February 16, 2007 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 Quote Link to comment Share on other sites More sharing options...
corbin Posted February 16, 2007 Share Posted February 16, 2007 I personally would stick with the AS because it's two more letters, and I don't see a reason to not use it...1/8th of a second of time is worth avoiding later hassles >.<. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.