phppup Posted May 21, 2012 Share Posted May 21, 2012 Can anybody clarify the usefulness of "AS" and elaborate on how it operates. If I select a 'bananas' AS 'yeelow fruit' will it change the TITLE in the table? Or the table that is being viewed? If I dump a list of 'FRUITS' into and HTML table, will using AS rename each column for me, or is that handled by MY coing of the HTML table? Quote Link to comment https://forums.phpfreaks.com/topic/262880-select-field-name-as-question/ Share on other sites More sharing options...
Kays Posted May 21, 2012 Share Posted May 21, 2012 ALIAS. It's useful if you use JOIN on tables that may have same column names. So you can alias the RESULT columns to have different names so that they don't clash. It won't modify your tables. Quote Link to comment https://forums.phpfreaks.com/topic/262880-select-field-name-as-question/#findComment-1347352 Share on other sites More sharing options...
Pikachu2000 Posted May 21, 2012 Share Posted May 21, 2012 If you SELECT a field from a table and use any MySQL functions while doing so, using an alias makes it easier to reference the field in an associative array when outputting the results. if you were to do this: $query = "SELECT DATE_FORMAT(date_field, '%M-%D-%Y') FROM table"; $result = mysql_query($query); You would need to access that field in an associative array like this: $array = mysql_fetch_assoc($result); echo $array['DATE_FORMAT(date_field, '%M-%D-%Y')']; With an alias, it becomes more straightforward: $query = "SELECT DATE_FORMAT(date_field, '%M-%D-%Y') AS mydate FROM table"; $result = mysql_query($query); $array = mysql_fetch_assoc($result); echo $array['mydate']; Quote Link to comment https://forums.phpfreaks.com/topic/262880-select-field-name-as-question/#findComment-1347361 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.