我有两张桌子:
表a:
DOCKER_HOST
表b:
PersonID | name | age | gender
-------- | ---- | --- | ------
20 | John | 45 | Male
30 | Ann | 50 | Female
我需要从表格' a'中获取所有值。每个对应的Column1都有一个.PersonID = b.PersonID和MAX ValueID。
此示例中的预期结果如下:
ValueID | PersonID | Column1
------- | -------- | -------
1 | 20 | 654321
2 | 20 | 345653
3 | 30 | 756756
4 | 30 | 142857
我试过这个并且不起作用。
PersonID | name | age | gender | Column1
-------- | ---- | --- | ------ -------
20 | John | 45 | Male | 345653
30 | Ann | 50 | Female | 142857
我猜WHERE子句SELECT ps.*,
(SELECT Column1 FROM b ef
JOIN (SELECT MAX(ValueID) AS id FROM b
WHERE PersonID = ps.PersonID) max ON ef.PersonID = max.id ) AS Column1
FROM a ps
中的ps.PersonID
无法识别。
我非常感谢你的帮助。
答案 0 :(得分:0)
试试这个..大概是valueid是唯一的/身份种子
SELECT ps.*,
(SELECT Column1 FROM b
where b.valueId =(SELECT MAX(ValueID) FROM b as x
WHERE x.PersonID = ps.PersonID)
) AS Column1
FROM a ps
答案 1 :(得分:-1)
试试这个:
SELECT ps.*,
(SELECT Column1
FROM b ef
JOIN (SELECT MAX(ValueID) AS id FROM b
WHERE PersonID = ps.PersonID) max ON ef.PersonID = max.id ) AS Column1
FROM a ps