简体   繁体   English

多次联接来自同一表的同一列

[英]Joining same column from same table multiple times

I need a two retrieve data from the same table but divided in different columns. 我需要两个从同一个表中检索数据,但分为不同的列。

First table " PRODUCTS " has the following columns: 第一个表“ PRODUCTS ”具有以下列:

  • PROD_ID PROD_ID
  • PRO_TYPE_ID PRO_TYPE_ID
  • PRO_COLOR_ID PRO_COLOR_ID
  • PRO_WEIGHT_ID PRO_WEIGHT_ID
  • PRO_PRICE_RANGE_ID PRO_PRICE_RANGE_ID

Second table " COUNTRY_TRANSLATIONS " has the following columns: 第二个表“ COUNTRY_TRANSLATIONS ”具有以下列:

  • ATTRIBUTE_ID ATTRIBUTE_ID
  • ATT_LANGUAGE_ID ATT_LANGUAGE_ID
  • ATT_TEXT_ID ATT_TEXT_ID

Third and last table " TEXT_TRANSLATIONS " has the following columns: 第三和最后一个表“ TEXT_TRANSLATIONS ”具有以下列:

  • TRANS_TEXT_ID TRANS_TEXT_ID
  • TRA_TEXT TRA_TEXT

PRO_TYPE_ID, PRO_COLOR_ID, PRO_WEIGHT_ID and PRO_PRICE_RANGE_ID are all integers and are found back in the column ATTRIBUTE_ID multiple times (depending on howmany translations are available). PRO_TYPE_ID,PRO_COLOR_ID,PRO_WEIGHT_ID和PRO_PRICE_RANGE_ID都是整数,可以在ATTRIBUTE_ID列中多次找到(取决于可用的翻译数量)。 Then ATT_TEXT_ID is joined with TRANS_TEXT_ID from the TEXT_TRANSLATIONS table. 然后,ATT_TEXT_ID与TEXT_TRANSLATIONS表中的TRANS_TEXT_ID联接。

Basically I need to run a query so I can retreive information from TEXT_TRANSLATIONS multiple times. 基本上,我需要运行查询,以便可以多次从TEXT_TRANSLATIONS中检索信息。 Right now I get an error saying that the correlation is not unique. 现在,我得到一个错误,指出相关性不是唯一的。

The data is available in more then 20 languages, therefore the need to work with intergers for each of the attributes. 可用超过20种语言提供数据,因此需要对每个属性使用整数。

Any suggestion on how I should build up the query? 关于如何建立查询的任何建议? Thank you. 谢谢。

Hopefully, you're on an RDBMS that supports CTEs (pretty much everything except mySQL), or you'll have to modify this to refer to the joined tables each time... 希望您使用的是支持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

Of course, personally I think the design of the localization table was botched in two ways - 当然,我个人认为本地化表的设计有两种缺陷:

  1. Everything is in the same table (especially without an 'attribute type' column). 一切都在同一个表中(尤其是没有“属性类型”列)。
  2. The language attribute is in the wrong table. 语言属性在错误的表中。

For 1), this is mostly going to be a problem because you now have to maintain system-wide uniqueness of all attribute values. 对于1),这将主要成为问题,因为您现在必须维护所有属性值在系统范围内的唯一性。 I can pretty much guarantee that, at some point, you're going to run into 'duplicates'. 我几乎可以保证,在某些时候,您将遇到“重复项”。 Also, unless you've designed your ranges with a lot of free space, the data values are non-consecutive for type; 另外,除非您为范围设计了足够的可用空间,否则数据值对于类型是不连续的; if you're not careful there is the potential for update statements being run over the wrong values, simply because the start and end of the given range belong to the same attribute, but not every value in the range. 如果您不小心,则可能会因为错误的值而运行update语句,这仅仅是因为给定范围的开始和结束属于同一属性,而不是该范围内的每个值。
For 2), this is because a text can't be completely divorced from it's language (and country 'locale'). 对于2),这是因为文本不能完全脱离其语言(和国家(地区)“ locale”)。 From what I understand, there are parts of some text that are valid as written in multiple languages, but mean completely different things when read. 据我了解,某些文本的某些部分可以用多种语言编写,但在阅读时却具有完全不同的含义。

You'd likely be better off storing your localizations in something similar to this (only one table shown here, the rest are an exercise for the reader): 您最好将本地化存储在与此类似的内容中(这里仅显示一个表,其余的是读者的练习):

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

This should make it so that all attributes have their own set of ids, and tie the localized text to what it was localized to directly. 这应该使所有属性都有自己的ID集,并将本地化的文本与本地化后的文本直接联系起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM