当前位置: 代码迷 >> Oracle管理 >> oracle 往ID最大的那条记录
  详细解决方案

oracle 往ID最大的那条记录

热度:44   发布时间:2016-04-24 04:57:32.0
oracle 去ID最大的那条记录
select  m.name,m.merchant_id,c.*
   from couponinfo c,merchant m, coupon_merchant cm 
   where c.coupon_id=cm.product_id
   and cm.merchant_id=m.merchant_id
   and c.coupon_name = 'text'

这样查出来是 多条数据! 如何取出 m.merchant_id 最大的那条记录
------最佳解决方案--------------------

  select rownum,t.* from
  (select m.name,m.merchant_id,c.*
  from couponinfo c,merchant m, coupon_merchant cm 
  where c.coupon_id=cm.product_id
  and cm.merchant_id=m.merchant_id
  and c.coupon_name = 'text'
  order by m.merchant_id desc) t
  where rownum=1;


没数据就没试,不知道行不行,你试试
------其他解决方案--------------------
select ROWNUM,m.name,m.merchant_id,c.*
  from couponinfo c,merchant m, coupon_merchant cm  
  where c.coupon_id=cm.product_id
  and cm.merchant_id=m.merchant_id
  and c.coupon_name = 'text' AND ROWNUM = 1
  ORDER BY m.merchant_id DESC
------其他解决方案--------------------
with t as (
select m.name,m.merchant_id,c.*
  from couponinfo c,merchant m, coupon_merchant cm  
  where c.coupon_id=cm.product_id
  and cm.merchant_id=m.merchant_id
  and c.coupon_name = 'text')
select * from t where merchant_id=(select max(merchant_id) from t);
 

------其他解决方案--------------------
不对啊 ! 执行报错 ora-01722
------其他解决方案--------------------
楼上的可以! 3 楼的 随即取了一条 但不是ID最大的
  相关解决方案