当前位置: 代码迷 >> Oracle管理 >> 使用子查询的有关问题
  详细解决方案

使用子查询的有关问题

热度:103   发布时间:2016-04-24 04:19:26.0
使用子查询的问题
select * from table1 where col1 in (select colxx from table2 where....)
这样的子查询是没有问题的
但是我想实现下面的子查询,可以吗?
select * from table1 where col1 > (select colxx from table2 where id=‘id’)
子查询的结果,我可以保证是一条(id为主键),但是好像不能这么用啊。
有什么方法能够代替吗?

要求在SQL语句中实现,不使用存储过程
------解决方案--------------------
引用:
select * from table1 where col1 in (select colxx from table2 where....)
这样的子查询是没有问题的
但是我想实现下面的子查询,可以吗?
select * from table1 where col1 > (select colxx from table2 where id=‘id’)
子查询的结果,我可以保证是一条(id为主键),但是好像不能这么用啊。
有什么方法能够代替吗?

要求在SQL语句中实现,不使用存储过程

如果你能保证子查询返回的是一条记录,并且返回的是number,那么这个语句是可以执行的。
如果实在不行的话你就用exists来实现吧

select *
  from table1 t1
 where exists (select 1
          from table2 t2
         where id = ‘id’
           and t1.col1 > t2.colxx)
  相关解决方案