MySQL - specific columns on join?

前端 未结 4 1171
抹茶落季
抹茶落季 2021-02-19 08:20

When making a join (inner, left outer, right outer or whatever), how can I specify which columns on the table to join into the original table?

Consider the following exa

相关标签:
4条回答
  • 2021-02-19 08:39

    You can specify the specific items to choose, like:

    SELECT User.FirstName, Provider.ID, Provider.Name FROM User LEFT OUTER JOIN Provider ON User.ProviderID = Provider.ID
    
    0 讨论(0)
  • 2021-02-19 08:46
      SELECT User.FirstName, Provider.ID, Provider.YourExtraColumnname, Provider.YourExtraColumnname2 FROM User LEFT OUTER JOIN Provider ON User.ProviderID = Provider.ID
    
    0 讨论(0)
  • 2021-02-19 08:48

    This will only include User.FirstName and Provider.ProviderID in the final resultset:

    SELECT User.FirstName, Provider.ProviderID FROM User LEFT OUTER JOIN Provider ON User.ProviderID = Provider.ID
    
    0 讨论(0)
  • 2021-02-19 09:03
             SELECT `User`.FirstName, Provider.* 
               FROM `User`
    LEFT OUTER JOIN Provider 
                 ON `User`.ProviderID = Provider.ID
    

    1. You use the table name before the column, or if you alias your tables, you can use the alias.

    E.g. LEFT OUTER JOIN Provider p and then you could access providers id on the select clause like this:

     SELECT `User`.FirstName, p.ID
    

    2. I added backticks around the table name User, because it is a reserved word for MySQL

    0 讨论(0)
提交回复
热议问题