![](/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.