grabbing first row in a mysql query only

前端 未结 3 1427
庸人自扰
庸人自扰 2020-12-08 14:04

if i had a query such as

select * from tbl_foo where name = \'sarmen\'

and this table has multiple instances of name = sarmen how can i vir

3条回答
  •  既然无缘
    2020-12-08 14:58

    You can get the total number of rows containing a specific name using:

    SELECT COUNT(*) FROM tbl_foo WHERE name = 'sarmen'
    

    Given the count, you can now get the nth row using:

    SELECT * FROM tbl_foo WHERE name = 'sarmen' LIMIT (n - 1), 1
    

    Where 1 <= n <= COUNT(*) from the first query.

    Example:

    getting the 3rd row

    SELECT * FROM tbl_foo WHERE name = 'sarmen' LIMIT 2, 1
    

提交回复
热议问题