2011-11-26 41 views
2

我有一個表,用於生成唯一ID的翻譯,名叫translations_ids translation_id(BIGINT)MySQL的選擇內容

一個表,這種結構命名translations_entries翻譯: translation_id(BIGINT) - 記者獨一無二translation_id在translations_ids 表

lang_abbr (varchar 3) 
    table_name (varchar 42) 
    translated_text (TEXT) 

表語言:

lang_abbr (varchar 3) 
    language (varchar 36) 
    title (varchar 36) 

這裏是表內容(例如新聞),這種結構:

id 
title (BIGINT) - correspondents to translation_id in translation_entries table 
content (BIGINT) - correspondents to translation_id in translation_entries table 
author (varchar 36) 
date (varchar36) 

我嘗試這樣做:

SELECT * 
FROM content LEFT JOIN translations_entries ON (content.title = translation_entries.translation_id AND 
content.content = translation_entries.translation_id) 
WHERE translations_entries.lang_abbr = 'en'; 

,但以這種方式,如果內容沒有任何的英文翻譯,它不會是在默認語言顯示

另一種方式是:

  1. SELECT * FROM content
  2. Foreach row SELECT * FROM translations_entries WHERE translation_id = this from content AND lang_abbr = 'en'
  3. 檢查行存在與默認語言其他新的查詢
  4. 當數據從兩個表並顯示新聞 但這種方式獲取的合併陣列可能會降低性能

我的問題是: 從給定語言的表格中選擇內容的最佳方式是什麼,如果該行的語言翻譯不存在,則選擇 默認語言的內容?

回答

1

對不起,我的第一個答案,這不是一個解決方案。 BTW

SELECT * 
FROM content LEFT JOIN (SELECT * FROM translations_entries 
WHERE translations_entries.lang_abbr = 'en') AS te 
ON (content.title = te.translation_id AND 
content.content = te.translation_id); 
+0

:試試這個,你還是沒有口碑投票向上或標記的答案是有效的,但請發表評論時,你可以讓我知道,如果它幫你,我很感興趣!謝謝;) – NotGaeL

+0

感謝您的答案,但這個查詢是類似於我的第一個查詢。如果任何一行內容僅在默認語言(bg)下翻譯且在en(英語 - 非默認語言)中沒有翻譯,則不會顯示翻譯。我想在en中選擇內容,如果en翻譯不存在一行或多行,然後以默認語言(bg)顯示它們。我的第一篇文章中描述的第二種方法是有效的,但如果有很多內容,這可能會降低性能,因爲查詢次數會更多。其他方案? – LightningBG

+0

其實如果你把where子句放在我告訴你它應該給出不同結果的地方,我嘗試着自己,它隻影響你加入的部分,所以你應該爲內容表中的每個元素獲得一行,或者用lang_abbr字段'en '或lang_abbr字段爲NULL,不含'en'翻譯。如果這不起作用,那麼我不明白你的表格結構。你能否進一步解釋你的命名約定?爲什麼content.title是translation_id而content.content也是translation_id?我認爲這個問題必須在你的ON條款中......並且你在哪裏保持默認的默認語言內容? – NotGaeL