create table tablename(id varchar(10),price int)
insert into tablename select 'a',1
insert into tablename select 'a',2
1insert into tablename select 'a',3
1insert into tablename select 'b',4go
select *
from tablename
where exists (select 1
from tablename where id='a')
--
查询记录是:a 1
a 2
a 3
b       4这样为什么不能得出指定ID的记录,而得出的是全部呢,select 1
from tablename where id='a')这句使用错误了吗

解决方案 »

  1.   

    exists  --如果查找到记录..就返回真.
      

  2.   

    select 1
    from tablename where id='a'当前这个条件为true 啊.所以是全部记录.
    --这样.
    select *
    from tablename where id='a' 
      

  3.   

    select top 1 from tablename where id='a'
      

  4.   

    楼主没必要那样写 你直接
    select *
    from tablename where  id='a'
    就可以哦!
      

  5.   

    lz应该品品exists的含义,是()里面有结果就返回真,否则返回假
      

  6.   

    理解一下exits的涵义它是只要存在就返回true..select *
    from tablename
    where id in (select 1
    from tablename where id='a')
      

  7.   


     where exists(select 1 from tablename where id='a')的意思是:只要tablename中有id='a'的记录 则选出tablename中的所有记录
      

  8.   


    select *
    from tablename
    where exists (select 1
    from tablename where id='a')
    --等同于
    go
    select *
    from tablename
    where exists (select 1
    from tablename where 1=1)
      

  9.   

    --改为
    select *
    from tablename a
    where exists (select 1
    from tablename where id=a.id and id='a')
      

  10.   

    select *
    from tablename
    where exists (select 1
    from tablename where id='a')与
    select *
    from tablename
    where exists (select *
    from tablename where id='a')性能一样的,因为执行过程它并没有返回Exists()里select的部分,只是判断是否为true 或 false
      

  11.   

    可以写成这种select *
    from tablename
    where exists (select 1
    from dual where id='a');其实和 select *
    from tablename
    where id='a' 一样
      

  12.   

    那如果是相關子查詢是不是每行數據都要進行true的判斷,還是只要第一次有true後面的就不用進行判斷了?
      

  13.   

    select *
    from tablename a
    where exists (select 1
    from tablename where id =a.id and id='a')--结果id     price
    --------------
    a 1
    a 2
    a 3
      

  14.   

    1.exist 返回值:true,false
       true 执行,false不执行
    2.select 1 from tablename where id='a'
    返回有值所以执行,显示所有记录.
    3.条件
    create table tablename(id varchar(10),price int)
    insert into tablename select 'a',1
    insert into tablename select 'a',2
    insert into tablename select 'a',3
    insert into tablename select 'b',4select 1
    from tablename where id='a'select *
    from tablename a
    where exists (select 1
    from tablename where id='a' and id = a.id)
      

  15.   

    这个问题你应该看exists而不是select 1