MySQL如何在包含缺少行的相同表上进行连接
我有一个表格,里面有各种语言的文本。其定义如下:MySQL如何在包含缺少行的相同表上进行连接,mysql,select,join,self-join,Mysql,Select,Join,Self Join,我有一个表格,里面有各种语言的文本。其定义如下: 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 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
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
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是关键
现在我想用一种语言(比如英语)提取所有文本,并将另一种语言(比如丹麦语)的协同文本显示在旁边的专栏中。因此,结果应该是:
0, a, b
1, c,
2, d, e
3, g
我知道我可以这样加入:
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
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)。如何做到这一点
***更新***
有人建议我使用左连接,但我无法让它工作。可能是因为我的表格布局与上面的简化问题有点不同。我的表格定义如下:
语言| MPageId | MFieldId | MParagraph |多行文字
其中语言,MPageId,MFieldId,MParagraph构成了关键
我试过这个:
选择t1.MPageId、t1.MFieldId、t1.MParagraphId、t1.MText、t2.MText
从main as t1向左连接main as t2 ON(t1.MPageId=t2.MPageId和
t1.MFieldId=t2.MFieldId和t1.MParagraphId=t2.MParagraphId),其中
t1.m语言='ENU'和t2.m语言='DAN'
您需要左连接:您确实需要左连接。。。但是“DAN”ish的“AND”子句将应用于左连接,而不是WHERE子句。。。where子句表示内部联接
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"
选择Id,
MAX(当LanguageS='ENU'然后Text else null end时的大小写)作为,
最大值(当语言为'ENU'然后文本为null end时为大小写)为B
来自局域网
按1分组
身份证
0 a b
1c?
二维e
3gf
我确实尝试过左键连接,但它没有满足我的要求。也许是因为我的数据库和我的问题有点不同。更新了我的问题。谢谢,我确实尝试了左加入,但它没有给我想要的,请检查我的更新