开发者

Only n records for every x field

Given this data set:

ID  Name            City            Birthyear
1   Egon Spengler   New York        1957
2   Mac Taylor      New York        1955
3   Sarah Connor    New York        1959
4   Jean-开发者_StackOverflow社区Luc Picard La Barre        2305
5   Ellen Ripley    Nostromo        2092
6   James T. Kirk   Nostromo        2233
7   Henry Jones     La Barre    1899

How can I get only 2 records from every city?


Try this:

SELECT i1.*
FROM your_table i1
LEFT JOIN your_table i2
  ON (i1.City = i2.City AND i1.ID > i2.ID)
GROUP BY i1.ID
HAVING COUNT(*) < 2
ORDER BY city, ID;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜