简体   繁体   中英

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:

  • PROD_ID
  • PRO_TYPE_ID
  • PRO_COLOR_ID
  • PRO_WEIGHT_ID
  • PRO_PRICE_RANGE_ID

Second table " COUNTRY_TRANSLATIONS " has the following columns:

  • ATTRIBUTE_ID
  • ATT_LANGUAGE_ID
  • ATT_TEXT_ID

Third and last table " TEXT_TRANSLATIONS " has the following columns:

  • TRANS_TEXT_ID
  • 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). Then ATT_TEXT_ID is joined with TRANS_TEXT_ID from the TEXT_TRANSLATIONS table.

Basically I need to run a query so I can retreive information from TEXT_TRANSLATIONS multiple times. 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.

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...

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. 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.
For 2), this is because a text can't be completely divorced from it's language (and country '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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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