当前位置: 代码迷 >> Java Web开发 >> SQL语句有有关问题,帮忙解决下··
  详细解决方案

SQL语句有有关问题,帮忙解决下··

热度:497   发布时间:2016-04-17 13:12:38.0
SQL语句有问题,帮忙解决下··
select   d_id,d_name,j_document.standId,stand_name,j_document.dt_id   from   scott.j_document   left   join   scott.j_stand   on   j_stand.stand_id   =   j_document.standId   where     d_state=1   and   rownum <   11   and   d_name   like   '%2% '   and   d_id   not   in(select   d_id   from   scott.j_document   where     d_state=1   and   d_name   like   '%2% '   and   rownum <1   order   by   d_id   desc)     order   by   d_id   desc  

看看这个语句哪里出错

提示是     “   order   by   d_id   desc)     order   by   d_id   desc   ”   前面那个order   前面那地方缺少右括号
但是把   前面那个“order   by   d_id   desc”   去掉就行了      
但我现在就要在子查询里面排序···
哪位帮忙解决下   ···

------解决方案--------------------
有一点不明白: not in 里面的子查询排序有什么意义?

------解决方案--------------------
select d_id from scott.j_document where d_state=1 and d_name like '%2% ' and rownum <1 order by d_id desc
上面是子查询,只作为一个not in的限定,是否有order by对结果没有影响的吧,还有 rownum <1这个条件看不明白, <1了,还查询啥,oracle默认rownum不是从1开始么,

所以这个not in是个空结果,所以感觉没啥意义


国内第一款“无端网游”―――猫游记,不用下客户端点击链接立即开始玩网游。开了IE就可以玩的。http://pet.mop.com/?u=8094002,用Mop的ID就能玩的,选择服务器的时候注意电信或网通哦
------解决方案--------------------
分析一下lz的意图:
1,子句:
select d_id from scott.j_document where d_state=1 and d_name like '%2% '
and rownum <1 order by d_id desc
lz想要的结果是满足条件的最大的一个d_id吧?

2,主句:
select d_id,d_name,j_document.standId,stand_name,j_document.dt_id from scott.j_document left join scott.j_stand on j_stand.stand_id = j_document.standId where d_state=1 and rownum < 11 and d_name like '%2% ' and d_id not in(子句) order by d_id desc
lz想要的是满足条件的最大的前10条数据吧?

结合1,2,lz的意图应该是d_state=1且d_name含有 '2 '的非最大d_id的最多共10条数据吧?
如果是这个意思的话你可以用max函数代替子句中的order by:

select d_id,d_name,j_document.standId,stand_name,j_document.dt_id from scott.j_document left join scott.j_stand on j_stand.stand_id = j_document.standId where d_state=1 and rownum < 11 and d_name like '%2% ' and d_id !=(select max(d_id) from scott.j_document where d_state=1 and d_name like '%2% ') order by d_id desc

不知道我理解得对不对?
  相关解决方案