现有一张表如下:
+------+------+-------+
| ID   | GID  | Price |
+------+------+-------+
|    1 |    3 |    23 |
|    1 |    4 |    25 |
|    2 |    3 |    26 |
|    2 |    4 |    32 |
+------+------+-------+
主键是 ID 和 GID,若想得到 GID=3,且 ID 最大的一条记录中的 Price(如上表中即为第3条记录),请问该如何查询?

解决方案 »

  1.   

    select a.* from tt a
    left join
    (select gid,max(id) as ma  from tt where gid='3'
    group by gid) b
    on a.gid=b.gid and a.id=b.ma
      

  2.   

    select a.* from tt a  where GID=3 and ID =(select max(id) from tt where gid='3'group by GID)
      

  3.   

    若想得到 GID=3,且 ID 最大的一条记录中的 Price
    select Price  from 一张表 where  GID=3 order by id desc limit 1;
      

  4.   

    select Price  from 一张表 where  GID=3 order by id desc limit 1;
      

  5.   

    select id,gid,price from {$tbl_name} where gid=3 group by gid,id order by id desc limit 1;
      

  6.   

    select distinct a.* from tt a  where GID=3 and ID =(select max(id) from tt where gid='3'group by GID)
      

  7.   

    select Price  from 表名 where  GID=3 order by ID desc limit 1;