select   ? from   ? where notes.nid= pbjtnote.nid and
      notes.typeid= pbjtnote.typeid and
      notes.userid= pbjtnote.userid
     想写这么一个查询语句。查询 notes和pbjtnote 表中 是否存在 类别、用户、编号相同的列。
select   count(nid) from   notes,pbjtnotewhere notes.nid= pbjtnote.nid and
      notes.typeid= pbjtnote.typeid and
      notes.userid= pbjtnote.userid
if count(nid)>0 then 
……
else ……
end if这样对吗?

解决方案 »

  1.   

    用 JOIN ?没有印象了 多说一点 谢谢
    我觉得用
    select count(*)into :n……
      

  2.   

    等等 不对吧
    这里怎么能用 JOIN ?
      

  3.   

    join 是连接 两个表用的!怎么能用在我这?
      

  4.   

    select  count(nid) from  notes,pbjtnote where notes.nid= pbjtnote.nid and 
          notes.typeid= pbjtnote.typeid and 
          notes.userid= pbjtnote.userid 
    if count(nid)>0 then 
    …… 
    else …… 
    end if 这样就可以了。大于0就存在
      

  5.   

    declare @i int
    select  @i=count(nid)
    from  notes,pbjtnotewhere notes.nid= pbjtnote.nid and
          notes.typeid= pbjtnote.typeid and
          notes.userid= pbjtnote.userid
    if @i>0 
    ....
    else if 
    .....
    else
    ....
      

  6.   

    select Count(*) from A where id=
      

  7.   


    declare @i int
    select  @i=count(nid)
    from  notes as a
    where exists
    (select 1 from pbjtnote as b where a.nid=b.nid and a.typeid=b.typeid and a.userid=b.userid)if @i>0 
    ....
    else if 
    .....
    else
    ....
      

  8.   

    我是这么写的
    select count(*)
    into :n
    from  notes,pbjtnotewhere notes.nid= pbjtnote.nid and
          notes.typeid= pbjtnote.typeid and
          notes.userid= pbjtnote.userid;
    然后就是判断n了
    当n<>0 是(已表示存在)
    update notes
    set comn=:ncom
    where notes.nid= pbjtnote.nid and
          notes.typeid= pbjtnote.typeid and
          notes.userid= pbjtnote.userid;但是执行上面更新时报
    SQLSTATE = 22005
    [Microsofte][odbc sql server driver]对于造型说明无效的字符
    notes 表结构是
    typeid varchar 20  主
    nid    int     4   主
    userid varchar 20  主
    uptime datetime 8  主
    comn   text    16  
      

  9.   

    用 JOIN 不是很简单的吗加个 if exist 测试一下就行了
    if exists  (select * from a join b on a.c1=b.c1 and a.c2=b.c2 and .......)
    不复杂吧
      

  10.   

    if exists  (select * from a join b on a.c1=b.c1 and a.c2=b.c2 and .......)
    用 exists 明白了。
    但是在程序里没有这么用过。
    if exists  (select * from a join b on a.c1=b.c1 and a.c2=b.c2 and .......) then
       update……//如果存在 就做更新
    else 
       insert……//否则 做插入
    end if
    这样对吗?
     
      

  11.   

    我写的 但是不行
    if exists  (select * from notes where notes.nid = :nid and notes.typeid =:typeid and notes.userid=:userid) thenupdate……//如果存在 就做更新 
    else 
      insert……//否则 做插入 
    end if 说明一下 nid typeid userid 这三个变量是当前的编号,种类编号,用户
    程序报Syntax error 和 Condition for if statement must be a boolean
    nid 是int 
      

  12.   

    不需要写 then 和 end if这是SQL,不是C语言
      

  13.   

    了解 
    但是我想写在程序里 
    你那exists 能用吗?求教了
    这是我写好的
    select count(*) 
    into :n 
    from notes 
    where notes.nid = :nid  and 
          notes.typeid = :typeid and 
          notes.userid=:userid ; 
    messagebox("",n) 
    你看看能用你的写吗?谢谢了