MySQL table alias names

willsherwood
willsherwood used Ask the Experts™
on
why does the first example below work, and the second complains of unknown field Sort

SELECT...  
        FROM PropertyListings AS PL , Towns AS T, Counties AS C                              
      WHERE PL.TownshipID = T.ID AND PL.CountyID = C.ID
      ORDER BY T.Sort DESC                               <---  (1)  WORKS

        ORDER BY Towns.Sort DESC                               <---  (2)  FAILS


thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Billing Engineer
Most Valuable Expert 2014
Top Expert 2009
Commented:
the second one fails, because once you use a table alias, you HAVE to use it everywhere in the query.

Commented:
so when the program encounters the t.fieldname it knows to take it from the town table

Author

Commented:
sorry my question was not clear lisa, i'm aware of the benefit of aliases,
but thanks angel, i did not know that defining/declaring an alias then deleted access for using the original table name.  I hear you, that just seems like an unreasonably strict language definition decision for MySQL.

thanks all
11/26 Forrester Webinar: Savings for Enterprise

How can your organization benefit from savings just by replacing your legacy backup solutions with Acronis' #CyberProtection? Join Forrester's Joe Branca and Ryan Davis from Acronis live as they explain how you can too.

Commented:
hi willsherwood

sorry should have explained better I meant it as when you declare anything as a name you must keep that name throughout so the program knows what field your are referencing

ie when it sees a t.whatever then it knows that its referencing the towns table

Author

Commented:
thanks.
i think the term "alias" is a misnomer  :)    

 (maybe a better term would be replacement or re-definition)
Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
I know of the same problems in MS SQL and Oracle ...  so it's not just a MySQL "issue"

Author

Commented:
thanks (sorry i thought i had closed this, but alas i had not confirmed)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial