我需要一个基于两个外键复制条目的SQL查询。为了解释我想要解决的问题,我在下面有一个简单的例子。我有两张桌子,一张有孩子,一张有父母(后面这里没有显示)。 children表有两个引用,一个给父亲,一个给父母表中的母亲:
(Table: "Children")
Name Father_ID Mother_ID
=============================
Adam 2 4
Emma 1 3
George 5 6
有一个名为“Parents”的表,其中Father_ID和Mother_ID都指向。这意味着父亲和母亲列在同一张桌子上。
现在我想要一个生成此结果的SQL查询:
Name Parent_ID
==================
Adam 2
Adam 4
Emma 1
Emma 3
George 5
George 6
即。重复条目的结果 - 每个父项一个。
我该如何解决?
答案 0 :(得分:0)
使用UNION ALL
:
select Name, Father_ID from Children
UNION ALL
select Name, Mother_ID from Children
如果需要,请添加ORDER BY Name
。
答案 1 :(得分:0)
试试这个
var lineSymbol = {
path: google.maps.SymbolPath.CIRCLE,
fillOpacity: 1,
scale: 3
};
polylineOptionsActual = new gm.Polyline({
strokeColor: '#9f98FF',
strokeOpacity: 0,
icons: [{
icon: lineSymbol,
offset: '0',
repeat: '10px'
}],
}),
答案 2 :(得分:0)
SELECT children.[Name], Mother_ID AS ParentId FROM Children JOIN parents ON children.Mother_ID = parents.ID
UNION
SELECT children.[Name], Father_ID AS ParentId FROM Children JOIN parents ON children.Father_ID = parents.ID