代码之家  ›  专栏  ›  技术社区  ›  Thomas Matthews

MySQL:删除左联接上的重复列,3个表

  •  11
  • Thomas Matthews  · 技术社区  · 15 年前

    我有一个表在其他表中使用了3个外键。当我执行左联接时,会得到重复的列。mysql说 USING 语法将减少重复列,但没有多个键的示例。

    鉴于:

    mysql> describe recipes;
    +------------------+------------------+------+-----+---------+-------+
    | Field            | Type             | Null | Key | Default | Extra |
    +------------------+------------------+------+-----+---------+-------+
    | ID_Recipe        | int(11)          | NO   | PRI | NULL    |       |
    | Recipe_Title     | char(64)         | NO   |     | NULL    |       |
    | Difficulty       | int(10) unsigned | NO   |     | NULL    |       |
    | Elegance         | int(10) unsigned | NO   |     | NULL    |       |
    | Quality          | int(10) unsigned | NO   |     | NULL    |       |
    | Kitchen_Hours    | int(10) unsigned | NO   |     | NULL    |       |
    | Kitchen_Minutes  | int(10) unsigned | NO   |     | NULL    |       |
    | Total_Hours      | int(10) unsigned | NO   |     | NULL    |       |
    | Total_Minutes    | int(10) unsigned | NO   |     | NULL    |       |
    | Serving_Quantity | int(10) unsigned | NO   |     | NULL    |       |
    | Description      | varchar(128)     | NO   |     | NULL    |       |
    | ID_Prep_Text     | int(11)          | YES  |     | NULL    |       |
    | ID_Picture       | int(11)          | YES  |     | NULL    |       |
    | Category         | int(10) unsigned | NO   |     | NULL    |       |
    | ID_Reference     | int(11)          | YES  |     | NULL    |       |
    +------------------+------------------+------+-----+---------+-------+
    15 rows in set (0.06 sec)
    
    mysql> describe recipe_prep_texts;
    +------------------+---------------+------+-----+---------+-------+
    | Field            | Type          | Null | Key | Default | Extra |
    +------------------+---------------+------+-----+---------+-------+
    | ID_Prep_Text     | int(11)       | NO   | PRI | NULL    |       |
    | Preparation_Text | varchar(2048) | NO   |     | NULL    |       |
    +------------------+---------------+------+-----+---------+-------+
    2 rows in set (0.02 sec)
    
    mysql> describe recipe_prep_texts;
    +------------------+---------------+------+-----+---------+-------+
    | Field            | Type          | Null | Key | Default | Extra |
    +------------------+---------------+------+-----+---------+-------+
    | ID_Prep_Text     | int(11)       | NO   | PRI | NULL    |       |
    | Preparation_Text | varchar(2048) | NO   |     | NULL    |       |
    +------------------+---------------+------+-----+---------+-------+
    2 rows in set (0.02 sec)
    
    mysql> describe mp_references;
    +--------------+---------+------+-----+---------+-------+
    | Field        | Type    | Null | Key | Default | Extra |
    +--------------+---------+------+-----+---------+-------+
    | ID_Reference | int(11) | NO   | PRI | NULL    |       |
    | ID_Title     | int(11) | YES  |     | NULL    |       |
    | ID_Category  | int(11) | YES  |     | NULL    |       |
    +--------------+---------+------+-----+---------+-------+
    3 rows in set (0.00 sec)
    

    我的查询语句:

    SELECT  *
     FROM  Recipes
    LEFT JOIN (Recipe_Prep_Texts, Recipe_Pictures, mp_References)
    ON (
     Recipe_Prep_Texts.ID_Prep_Text = Recipes.ID_Prep_Text AND
     Recipe_Pictures.ID_Picture = Recipes.ID_Picture AND
     mp_References.ID_Reference = Recipes.ID_Reference
    );
    

    我的目标是从联接中获取所有列中的一行,而不复制列。我使用MySQL C++连接器发送SQL语句并检索结果集。我相信C++连接器有重复列名的问题。

    那么我应该使用什么样的SQL语句语法呢?

    Reference to MySQL JOIN syntax

    4 回复  |  直到 9 年前
        1
  •  13
  •   Danny Shisler    15 年前

    我认为以下方法应该有效:

    SELECT  *
     FROM  Recipes
    LEFT JOIN Recipe_Prep_Texts USING (ID_Prep_Text)
    LEFT JOIN Recipe_Pictures USING (ID_Picture)
    LEFT JOIN mp_References USING (ID_Reference)
    
        2
  •  4
  •   Rob Van Dam    15 年前

    因为看起来您要连接的大多数表都有一些列,除了第一列之外,如何处理:

       SELECT Recipes.*,
              Recipe_Prep_Texts.Preparation_Text,
              Recipe_Pictures.Foo, -- describe is missing in OP
              mp_References.ID_Title,
              mp_References.ID_Category
    
         FROM Recipes
    LEFT JOIN (Recipe_Prep_Texts, Recipe_Pictures, mp_References)
           ON (
               Recipe_Prep_Texts.ID_Prep_Text = Recipes.ID_Prep_Text AND
               Recipe_Pictures.ID_Picture = Recipes.ID_Picture AND
               mp_References.ID_Reference = Recipes.ID_Reference
              );
    

    我不能告诉你我希望我有多少次

    SELECT (* - foo) FROM table
    

    尤其是在foo是一个像blob这样的大字段的情况下,我只想在不破坏格式的情况下看到其他所有内容。

        3
  •  2
  •   tgandrews    15 年前

    您正在从组合结果表中选择*。将*限制在您要保留的列中。

        4
  •  0
  •   Mike F    9 年前

    尝试以下查询:

    SELECT  name,ac,relation_name
    FROM  table1
    LEFT JOIN table2 USING (ID_Prep_Text)
    LEFT JOIN table3 USING (ID_Picture);
    
    推荐文章