开发者

SQL Statement, always list 3 entries of a sort

I have another Problem

I just can show you this on an example:

I got a Table called "myTable" with 7 entries

myTable | id | user_id |
           1      1 
           2      1
           3      1
           4      1 
           5      2
           6      2 
           7      2  
           8      2  
           9      2  
           10     2  

Now i want to list just 3 entries of a开发者_Python百科ny user_id, but i cant find out myself how to.

Please help!

------- EDIT

My result have to look like this:

id=1 | user_id=1
id=2 | user_id=1
id=3 | user_id=1
id=5 | user_id=2
id=6 | user_id=2
id=7 | user_id=2


select * from table as t1
where (select count(*) from table as t2
       where t1.user_id = t2.user_id and t2.id < t1.id) <3


SELECT *
FROM table
ORDER BY ...
LIMIT 3

The LIMIT clause takes two arguments: number of rows to return, and an offset if you want to display something other than the first X rows. Details here in the MySQL docs.


If you want only 3 rows, you can use limit :

select * from myTable limit 3;


SELECT * FROM table LIMIT 3

http://dev.mysql.com/doc/refman/5.0/en/select.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜