1

我有两张表——一张是库存记录,另一张是翻译(供法语和德语用户使用)。

库存:

╔════╦═══════╦═══════════════════╦═══════════════════════════╗
║ ID ║ PRICE ║ ITEMTRANSLATIONID ║ ITEMCATEGORYTRANSLATIONID ║
╠════╬═══════╬═══════════════════╬═══════════════════════════╣
║  1 ║    10 ║               423 ║                      1323 ║
║  2 ║    31 ║              1776 ║                      1953 ║
╚════╩═══════╩═══════════════════╩═══════════════════════════╝

翻译:

╔══════╦═══════════╦════════════╦═════════╗
║  ID  ║  ENGLISH  ║   FRENCH   ║ GERMAN  ║
╠══════╬═══════════╬════════════╬═════════╣
║    1 ║ knife     ║ couteau    ║ messer  ║
║    2 ║ fork      ║ fourchette ║ gabel   ║
║  423 ║ spoon     ║ cuillère   ║ löffel  ║
║ 1323 ║ cultery   ║ couverts   ║ besteck ║
║ 1776 ║ table     ║ table      ║ tabelle ║
║ 1953 ║ furniture ║ meubles    ║ möbel   ║
╚══════╩═══════════╩════════════╩═════════╝

有没有办法编写 SQL 查询来获取每个库存项目的价格和翻译名称?我一次只需要一种语言。

如果只有一列需要翻译,我可以使用INNER JOIN. 问题是,有两列需要翻译——一列用于项目名称,另一列用于项目类别名称。

即所需输出(法语)

╔════╦═══════╦══════════╦══════════════╗
║ ID ║ PRICE ║   ITEM   ║ ITEMCATEGORY ║
╠════╬═══════╬══════════╬══════════════╣
║  1 ║    10 ║ cuillère ║ couverts     ║
║  2 ║    31 ║ table    ║ meubles      ║
╚════╩═══════╩══════════╩══════════════╝
4

3 回答 3

2

在表上连接表Translations两次,Stock这样您就可以获得表中每一列的值Stock

SELECT  a.ID, a.Price, b.French AS Item, c.French AS ItemCategory  
FROM    Stock a
        INNER JOIN Translations b
            ON a.ItemTranslationId = b.ID
        INNER JOIN Translations c
            ON a.ItemCategoryTranslationId = c.ID
于 2013-01-10T15:52:01.663 回答
1

使用此表结构,您需要对JOIN表进行Translations两次...一次获取Item,再次获取ItemCategory

SELECT
    s.ID,
    s.Price,
    i.French AS Item,
    ic.French AS ItemCategory
FROM
    Stock s
    JOIN Translations i ON i.ID = s.ItemTranslationId
    JOIN Translations ic ON ic.ID = s.ItemCategoryTranslationId 
于 2013-01-10T15:51:24.737 回答
0

您可以使用此查询:

SELECT  a.ID, a.Price,
(select French from  Translations b where b.ID=a.ItemTranslationId) as ITEM,
(select French from  Translations c where c.ID=a.ItemCategoryTranslationId) as ITEMCATEGORY
FROM Stock a
于 2013-02-14T09:31:24.133 回答