Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL left join with multiple rows into one row

Basically, I have two tables, Table A contains the actual items that I care to get out, and Table B is used for language translations.

So, for example, Table A contains the actual content. Anytime text is used within the table, instead of storing actual varchar values, ids are stored that relate back to text stored in Table B. This allows me to by adding a languageID column to Table B, have multiple translations for the same row in the database.

Example:

Table A

  • Title (int)
  • Description (int)
  • Other Data....

Table B

  • TextID (int) - This is the column whose value is stored in other tables
  • LanguageID (int)
  • Text (varchar)

My question is more a call for suggestions on how to best handle this. Ideally I want a query that I can use to select from the table, and get the text as opposed to the ids of the text out of the table. Currently when I have two text items in the table this is what I do:

SELECT C.ID, C.Title, D.Text AS Description
FROM
(SELECT A.ID, A.Description, B.Text AS Title
FROM TableA A, TranslationsTable B
WHERE A.Title = B.TextID AND B.LanguaugeID = 1) C
LEFT JOIN TranslationsTable D
ON C.Description = D.TextID AND D.LanguaugeID = 1

This query gives me the row from Table A I am looking for (using where statements in the inner select statement) with the actual text based on the language ID used instead of the text ids.

This works fine when I am only using one or two text items that need to be translated, but adding a third item or more, it starts to get really messy - essentially another left join on top of the example.

Any suggestions on a better query, or at least a good way to handle 3 or more text items in a single row?

Per suggestions, I've added an example of the two tables:

    Table A  
    ---------------------------
    ID    |Title    |Description  
    ---------------------------  
    1     |1        |2  
    ---------------------------  
    2     |3        |4  
    --------------------------- 

    Table B (Translations Table) 
    ---------------------------
    ID    |LanguaugeID|Text  
    ---------------------------  
    1     |1        |Here is title one
    ---------------------------  
    1     |2        |Here is a title one in espanol
    ---------------------------
    2     |1        |Here is description one
    ---------------------------  
    2     |2        |Here is description one in espanol
    ---------------------------
    3     |1        |Title 2
    ---------------------------  
    4     |1        |Description 2
    ---------------------------   

What I want is to be able to pull a row out of table A that already has the text from table B, not just the ids - and to be able to do this for several columns that need translations.

like image 901
beardedd Avatar asked Apr 01 '10 14:04

beardedd


1 Answers

It sounds like you want would benifit from converting some of the row data to column data. In that case look up the PIVOT functionality here

http://msdn.microsoft.com/en-us/library/ms177410.aspx

You could write a query to PIVOT out the text so that you can get the following output

ID, Title, Description, LanguageId, Text1, Text2, Text3, Text4

The only disadvantage to a PIVOT in TSQL is that you have to identify the number of pivot columns beforehand (when you write the query). You can, however, overcome this by writing dynamic SQL.

like image 185
Raj More Avatar answered Sep 21 '22 02:09

Raj More