我有一张表格,上面有各种语言的文本。它的定义是这样的:

Id|Language|Text

EXAMPLE DATA

0, ENU, a
0, DAN, b
1, ENU, c
2, ENU, d
2, DAN, e
3, ESP, f
3, ENU, g

语言和身份构成了关键。

现在我想提取一种语言(比如说英语)中的所有文本,并在旁边的列中显示另一种语言(比如说丹麦语)中的对应文本。所以结果应该是:

0, a, b
1, c, 
2, d, e
3, g

我知道我可以像这样加入:

SELECT t1.Id, t1.Text AS "ENU", t2.Text AS "DAN" table as t1 
JOIN table as t2 ON (t1.Id= t2.Id) 
WHERE t1.Langauge = "ENU" AND t2.Language = "DAN";

但这不包括缺失的行(即行 id=1 和 id=3)。这个怎么做?

* 更新 ****我收到使用 LEFT JOIN 的建议,但我无法让它工作。也许是因为我的表格布局与上面的简化问题有点不同。我的表定义如下:

语言|MPageId|MFieldId|MParagraph|MText

其中 Language、MPageId、MFieldId、MParagraph 构成键

我试过这个:

选择t1.mpageid,t1.mfieldid,t1.mparagraphid,t1.mtext,t2.mtext,t2.mtext,t1 = t2.mparagraphid)其中t1.mlanguage ='enu'和t2.mlanguage ='dan'

有帮助吗?

解决方案

SELECT t1.Id, t1.Text AS "ENU", t2.Text AS "DAN" FROM table as t1 
LEFT JOIN table as t2 ON (t1.Id= t2.Id AND t2.Language = "DAN")
WHERE t1.Langauge = "ENU"

其他提示

你确实需要左连接...但“DAN”的“AND”子句将应用于 LEFT JOIN,而不是应用于 WHERE 子句...where 子句隐含 INNER JOIN

SELECT 
      t1.Id, 
      t1.Text AS "ENU", 
      t2.Text AS "DAN" 
   from
      YourTable t1
         LEFT JOIN YourTable t2 
            ON t1.Id= t2.Id
           AND t2.Language = "DAN"
   where
      t1.Langauge = "ENU"
select Id,
MAX(case when LanguageS='ENU' then Text  else null end  ) as A,
MAX( case when LanguageS<>'ENU' then Text  else null end ) as B
from LAN
GROUP BY 1

Id      A           B
0       a           b
1       c           ?
2       d           e
3       g           f
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top