1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How should I understand all of these letters for this query?

Discussion in 'MySQL' started by JoshuaEir, Dec 6, 2020.

  1. #1
    I am working on SQL bridge tables. How do I understand this query? I don't understand all of the letters. Thanks a bunch.

    Josh
     

    Attached Files:

    Solved! View solution.
    JoshuaEir, Dec 6, 2020 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    The letters are "aliases" and save the programmer from having to repeat the table name, and allows them to use a table twice in the same query

    therefore
    select d.Build_name as 'Building', b.type as 'Energy Type', a.category as 'Energy Category' from EnergyCategories as a...
    Code (markup):
    could also be written as
    select Buildings.Build_name as 'Building', EnergyTypes.type as 'Energy Type', EnergyCategories.category as 'Energy Category' from EnergyCategories...
    Code (markup):
    The left joins describe how the tables relate to each other.
     
    sarahk, Dec 6, 2020 IP
  3. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    818
    Best Answers:
    7
    Trophy Points:
    320
    #3
    The letters like "a." are nothing more than abbreviations for the table names.
     
    mmerlinn, Dec 6, 2020 IP
  4. JoshuaEir

    JoshuaEir Member

    Messages:
    59
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    28
    #4
    What does the last : ...'from EnergyCategories as a' do?
    Thanks again.
     
    JoshuaEir, Dec 6, 2020 IP
  5. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    818
    Best Answers:
    7
    Trophy Points:
    320
    #5
    It defines 'a' just like 'd' is defined by Buildings.
     
    mmerlinn, Dec 6, 2020 IP
  6. #6
    Lets start with whoever wrote the query isn't an expert...

    From my code examples above I'd prefer to see something like this:
    SELECT b.Build_name, et.type, ec.category
    FROM EnergyCategories AS ec
    LEFT JOIN EnergyTypes AS et ON ec.cat_id = et.cat_id
    LEFT JOIN BETypes AS bet ON et.type_id = bet.type_id
    LEFT JOIN Buildings AS b ON bet.Build = b.Build
    WHERE ec.category = 'Renewable'
    Code (markup):
    So what I've done is make the alias (short name) match the table name.
    a, b, c , d is nonsense.
    ec, et, bet, b make sense if you know the tables that you're working with. You should be able to read the query and intuitively know what the alias is referring to. You're new to this so it may still be hard now, but it will get easier.

    I've also removed the alias names for the fields you're selecting. I usually only use them on aggregated fields (count, case, etc) or where I'm selecting two fields with the same name (id, name, notes, etc) - or if I'm passing the results to another tool like a csv generator that will use the field names as column headings.
     
    Last edited: Dec 8, 2020
    sarahk, Dec 7, 2020 IP
  7. JoshuaEir

    JoshuaEir Member

    Messages:
    59
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    28
    #7
    Thanks everyone...so busy today.
     
    JoshuaEir, Dec 8, 2020 IP