2011-11-08 2 views
0

表交換カラムに参加 - content:変換情報を保持している複雑MySQLのコンテンツ情報を保持している

+----------+-------------+-------------------+--------------------------------+ 
| (int) id | (int) title | (int) description | (string) tags     | 
+----------+-------------+-------------------+--------------------------------+ 
|  1 |   12 |    18 | super, awesome, must-see  | 
+-----------------------------------------------------------------------------+ 
|  4 |   25 |    26 | randomness, funny-stuff, cool | 
+-----------------------------------------------------------------------------+ 

表 - translations

+-----------+---------------------------------+----------------+ 
| (int) tid | (text) value     | (varchar) code | 
+-----------+---------------------------------+----------------+ 
|  12 | Super-awesome-mustsee   | en    | 
+--------------------------------------------------------------+ 
|  18 | <here would be the description> | en    | 
+--------------------------------------------------------------+ 
| <more translation data that is not necessary for this xmpl.> | 
+--------------------------------------------------------------+ 

私が達成したい何をしてcontent.titleを交換すること、ですtranslations.valueと説明のために同じ(異なる構成要素(content)テーブルのより多く/より少ないデータ)これまでのところ、私は一つの値だけのための翻訳データを結合するために持っている

+----------+-----------------------+---------------------------------+--------------------------------+ 
| (int) id | (text) title   | (text) description    | (string) tags     | 
+----------+-----------------------+---------------------------------+--------------------------------+ 
|  1 | Super-awesome-mustsee | <here would be the description> | super, awesome, must-see  | 
+-----------------------------------------------------------------------------------------------------+ 

...そして、はい、参加代わるものではありませ:のように0、translations.tidと一致します。 :|

SELECT `content` . * , `translations`.value 
FROM `content` 
JOIN `translations` ON `translations`.tid = `content`.title 
WHERE `translations`.code = 'en' 

どうすればよいですか?

ありがとうございます!

+0

この行:JOIN 'translations' ON' translations'.tid = 'content'.titleは間違っています:あなたはcontent.idに参加しなければなりません。タイトルではなく... – Nanocom

答えて

2

そう複雑ではありません:

SELECT 
    `c`.`id`, 
    `tt`.`value` AS title, 
    `td`.`value` AS description, 
    `c`.`tags` 
FROM 
    `content` `c` 
LEFT JOIN 
    `translations` `tt` ON (`tt`.`tid` = `c`.`title` AND `tt`.`code` = 'en') 
LEFT JOIN 
    `translations` `td` ON (`td`.`tid` = `c`.`description` AND `td`.`code` = 'en') 

基本的には*をドロップし、結合されたテーブルからの正確な列とそのエイリアスを指定する必要があります。 2番目のことは、同じテーブルでダブルジョインを作成しなければならないということです.1つはタイトルの翻訳を取得し、2番目は説明を取得します。結果を「置換」するには、単にvalue列の別名を使用します。

+0

ああ、ありがとう、魅力のように働く。あなたは 'ON'ステートメントをグループ化できることを知らなかった。 – jolt

関連する問題