有一个表(location):
id  x   y   kind  kindid 
-----------------------
5  500 500  1     57
9  500 500  251   1
里面有两条记录,我已经知道两条记录的kind和kindid字段的值.我想判断两条记录的x和y是不是一样的.IF EXISTS(..这条sql语句应该怎么写???...)谢谢了 

解决方案 »

  1.   


    if (select x from tb where kind=1 and kindid =57)=(select x from tb where kind=251 and kindid =1) and (select y from tb where kind=1 and kindid =57)=(select y from tb where kind=251 and kindid =1)print 'x、y都相等'
      

  2.   


    create table test(id int,x int,y int,kind int,kindid int)
    insert test
    select 5,500,500,1,57 union all
    select 9,500,500,251,1
    go--1代表相等 0代表不等select count(*) from test
    where x=y and kind = 1 and kindid = 57            
    ----------- 
    1(所影响的行数为 1 行)
      

  3.   


    if exists(select * from tb t where exists(select 1 from tb where kind=251 and kindid =1 and x=t.x an y=t.y)and kind=1 and kindid =57)
    print 'x、y都相等'
      

  4.   


    declare @t table(id int,x int,y int,kind int,kindid int)
    insert @t
    select 5,500,500,1,57 union all
    select 9,500,500,251,1
    if exists(select * from @t t where exists(select 1 from @t where kind=251 and kindid =1 and x=t.x and y=t.y)and kind=1 and kindid =57) print 'x、y都相等'else
    print 'x、y不相等'---------------------------x、y都相等
      

  5.   

    IF EXISTS
    (
    (select x from location where kind=1 and kindid=57) = (select x from location where kind=251 and kindid=1) 
    and 
    (select y from location where kind=1 and kindid=57) = (select y from location where kind=251 and kindid=1)
    )非常感谢:
    js_szy,明天我到单位去测试一下.可以后马上给份哈
      

  6.   


    declare @t table(id int,x int,y int,kind int,kindid int)
    insert @t
    select 5,500,500,1,57 union all
    select 9,400,500,251,1              ----修改X为400,不一样了
    if exists(select * from @t t where exists(select 1 from @t where kind=251 and kindid =1 and x=t.x and y=t.y)and kind=1 and kindid =57) print 'x、y都相等'else
    print 'x、y不相等'---------------------------x、y不相等
      

  7.   

    fredrickhu,天天都在呀.我需要判断,上面只是例子数据,实际我用在存储过程中,判断两个对象的坐标是否在同一个地点.
      

  8.   

    1楼的那个就不要再加exists了用exists,就用6楼或8楼的
      

  9.   

    if OBJECT_ID('tb') is not null drop table tb
    create table tb(id int,x int,y int,kind int,kindid int)
    insert tb
    select 5,500,500,1,57 union all
    select 9,500,500,251,1
    go
    if exists(select * from tb t where
                  exists(select 1 from tb where kind=251 and kindid =1 and x=t.x and y=t.y) and kind=1 and kindid =57)
      print 'x、y都相等'
    else 
      print 'x,y不等
      

  10.   

    用CASE WHEN 就可以了!
      

  11.   

    if OBJECT_ID('tb') is not null drop table tb
    create table tb(id int,x int,y int,kind int,kindid int)
    insert tb
    select 5,500,500,1,57 union all
    select 9,500,500,251,1
    goif exists(select 1 from tb a where  x=y and (kind=251 and kindid =1) or (kind=1 and kindid=57))
      print 'x、y都相等'
    else 
      print 'x,y不等'
    --x、y都相等最简单的方法!呵呵!
      

  12.   


    不用exists的话,1楼的可以一定要用exists的话。6楼或者8楼的都可以
      

  13.   


    DECLARE @t TABLE(id INT,x INT,y INT,kind INT,kindid INT)
    INSERT @t
    SELECT 5,500,500,1,57 UNION ALL
    SELECT 9,500,500,251,1IF EXISTS(SELECT * FROM @t A 
               INNER JOIN @t B ON A.x = B.x AND A.y = B.y
               WHERE A.kind = 1 AND A.kindid = 57
                 AND B.kind = 251 AND B.kindid = 1)    PRINT 'x、y都相等'ELSE
        PRINT 'x、y不相等'
      

  14.   

    id x y kind  kindid 
    -----------------------
    5 400 500  6   57
    9 400 500  251  1
      

  15.   

    感觉这样可以,感谢大家了.if exists
    (select * from location t where exists(select 1 from location where kind=251 and kindid=1 and x=t.x and y=t.y) and kind=1 and kindid=57)