2009-12-24 378 views
10

我有一個表使用3個外鍵到其他表中。當我執行左連接時,我會得到重複的列。 MySQL表示,USING語法將減少重複列,但沒有多個鍵的示例。MySQL:刪除左連接上的重複列,3個表

考慮:

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

回答

13

我相信下面應該工作:

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

這適用,除非表不存在。所以我進行了檢查。此外,這假定表具有唯一的列名稱,除了外鍵字段。幸運的是,這就是我所擁有的。 **非常感謝!** – 2009-12-24 19:37:19

2

您從合併結果表中選擇*。將*限制在你想保留的任何列。

+1

正如你在從MySQL後的數據看,我有很多的列。如果必須列出除重複項以外的所有列,這可能會很痛苦。是否有一個SQL限定符可讓我列出除指定的列之外的所有列? – 2009-12-24 05:51:59

+0

不是我所知道的。 – tgandrews 2009-12-24 09:17:01

4

因爲它看起來像最要加入的表都有,除了第一個幾列,怎麼樣:

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這樣龐大的字段的情況下,我只想在不中斷格式的情況下查看其他所有內容。

+2

儘管枚舉每個有效的列是可能的,但我認爲這對我的程序來說是很多工作,並且還增加了發送到數據庫的SQL語句的長度。請參閱下面Danny的回答。 – 2009-12-24 19:39:01

0

嘗試以下查詢:

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