2009-12-24 13 views
10

Diğer tablolara 3 yabancı anahtar kullanan bir tablom var. Sol bir katılım gerçekleştirdiğimde, yinelenen sütunları alıyorum. MySQL, USING sözdiziminin yinelenen sütunları azaltacağını, ancak birden çok tuşa örnek olmadığını söylüyor.MySQL: Sol Katmanda çift sütunları kaldırma, 3 tablo

Verilen:

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) 

Benim sorgu ifadesi:

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 
); 

Benim amacım yinelenen sütun olmadan katılmak tüm sütunların bir satır elde etmektir. SQL deyimlerini göndermek ve sonuç kümelerini almak için MySQL C++ Bağlayıcısı kullanıyorum. C++ Bağlayıcısı, yinelenen sütun adlarıyla ilgili sorunlara sahip olduğuna inanıyorum.

Kullanmam gereken SQL deyim sözdizimi nedir?

Reference to MySQL JOIN syntax

cevap

13

Ben şu çalışması gerekir inanıyoruz:

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

Bu, tablonun mevcut olmadığı durumlar dışında çalışır. Ben de bunun için kontroller yaptım. Ayrıca, bu, tabloların yabancı anahtar alanları dışında benzersiz sütun adlarına sahip olduğunu varsayar. Neyse ki, sahip olduğum şey bu. **Çok teşekkürler!** –

2

Sen kombine çıkan tablodan * seçiyoruz. Tutmak istediğiniz sütunları * sınırlayın.

+1

Eğer MySQL post verilerinde baktığımda, sütunların çok şey var. Çoğaltmalar dışındaki tüm sütunları listelemem gerekirse bu bir acı olabilir. Belirttiklerim dışındaki tüm sütunları listelememe izin verecek bir SQL niteleyicisi var mı? –

+0

Bildiğim kadarıyla değil. – tgandrews

4

üzerinde katılan tabloların çoğu İlki hariç birkaç sütun varmış gibi görünüyor beri nasıl:

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 
     ); 

ben

olsaydı kaç kez söyleyemem
SELECT (* - foo) FROM table 

özellikle foo'nun BLOB gibi büyük bir alan olduğu durumlarda ve sadece biçimlendirmeyi bozmadan her şeyi görmek istiyorum.

+2

Her geçerli sütunu numaralandırmak mümkün olsa da, programım için çok fazla çalışma olduğunu düşünüyorum ve ayrıca veritabanına gönderilen SQL deyiminin uzunluğunu eklerim. Danny'nin cevabını aşağıda gör. –

0

aşağıdaki sorguyu deneyin:

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