![](/img/trans.png)
[英]SQL joining the same table multiple times on the same column with differing restrictions
[英]Joining same column from same table multiple times
我需要兩個從同一個表中檢索數據,但分為不同的列。
第一個表“ PRODUCTS ”具有以下列:
第二個表“ COUNTRY_TRANSLATIONS ”具有以下列:
第三和最后一個表“ TEXT_TRANSLATIONS ”具有以下列:
PRO_TYPE_ID,PRO_COLOR_ID,PRO_WEIGHT_ID和PRO_PRICE_RANGE_ID都是整數,可以在ATTRIBUTE_ID列中多次找到(取決於可用的翻譯數量)。 然后,ATT_TEXT_ID與TEXT_TRANSLATIONS表中的TRANS_TEXT_ID聯接。
基本上,我需要運行查詢,以便可以多次從TEXT_TRANSLATIONS中檢索信息。 現在,我得到一個錯誤,指出相關性不是唯一的。
可用超過20種語言提供數據,因此需要對每個屬性使用整數。
關於如何建立查詢的任何建議? 謝謝。
希望您使用的是支持CTE的RDBMS(除mySQL以外幾乎所有其他東西),或者您每次都必須修改它以引用聯接的表...
WITH Translations (attribute_id, text)
as (SELECT c.attribute_id, t.tra_text
FROM Country_Translations c
JOIN Text_Translations t
ON t.trans_text_id = c.att_text_id
WHERE c.att_language_id = @languageId)
SELECT Products.prod_id,
Type.text,
Color.text,
Weight.text,
Price_Range.text
FROM Products
JOIN Translations as Type
ON Type.attribute_id = Products.pro_type_id
JOIN Translations as Color
ON Color.attribute_id = Products.pro_color_id
JOIN Translations as Weight
ON Weight.attribute_id = Products.pro_weight_id
JOIN Translations as Price_Range
ON Price_Range.attribute_id = Products.pro_price_range_id
當然,我個人認為本地化表的設計有兩種缺陷:
對於1),這將主要成為問題,因為您現在必須維護所有屬性值在系統范圍內的唯一性。 我幾乎可以保證,在某些時候,您將遇到“重復項”。 另外,除非您為范圍設計了足夠的可用空間,否則數據值對於類型是不連續的; 如果您不小心,則可能會因為錯誤的值而運行update語句,這僅僅是因為給定范圍的開始和結束屬於同一屬性,而不是該范圍內的每個值。
對於2),這是因為文本不能完全脫離其語言(和國家(地區)“ locale”)。 據我了解,某些文本的某些部分可以用多種語言編寫,但在閱讀時卻具有完全不同的含義。
您最好將本地化存儲在與此類似的內容中(這里僅顯示一個表,其余的是讀者的練習):
Color
=========
color_id -- autoincrement
cyan -- smallint
yellow -- smallint
magenta -- smallint
key -- smallint
-- assuming CYMK palette, add other required attributes
Color_Localization
===================
color_localization_id -- autoincrement, but optional:
-- the tuple (color_id, locale_id) should be unique
color_id -- fk reference to Color.color_id
locale_id -- fk reference to locale table.
-- Technically this is also country dependent,
-- but you can start off with just language
color_name -- localized text
這應該使所有屬性都有自己的ID集,並將本地化的文本與本地化后的文本直接聯系起來。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.