0

我有一张名为People Job (ID, People ID, Post).

有样本数据。

ID   People ID      Post

1    P1         Office Assistant

2    P2         Assistant Manager

3    P3         General Manager

4    P1         Officer

5    P2         Manager

我想获得如下记录。

ID    People ID     Post

3    P3          General Manager

4    P1          Officer

5    P2          Manager
4

3 回答 3

3
SELECT PJ.*
FROM People_Job PJ
INNER JOIN (
    SELECT People_ID, MAX(ID) MAX_ID
    FROM People_Job
    GROUP BY People_ID
) T ON PJ.People_ID = T.People_ID AND PJ.ID = T.MAX_ID
于 2013-08-01T09:53:23.093 回答
1

不太确定您的要求,但猜测您想要特定 people_id 的最高 id 的数据......这应该适合:

select * 
from People_Job
where id in (
    select max(id)
    from People_Job
    group by people_id
);

我刚看到 GriGrim 的帖子——如果你在大数据上运行,他的版本应该会表现得更好......

于 2013-08-01T09:58:18.723 回答
0
SELECT * FROM `People Job` WHERE `ID People`>2 ORDER BY `ID People`

但是您的表和列名不应包含空格。

于 2013-08-01T09:54:11.137 回答