遇到个问题,向大家请教,表的结构如下id name
1  lily
1  tom
1  john
2  lily
2  tom
2  john
... ...
... ...
n  lily
n  tom
n  john现在要判断所有id的对应的name必须都含有lily tom john,并且顺序必须为lily tom john
我自己的办法是 select name from students where id = 1
把id为1的名字都取出来,依次和lily tom john 做比较。
这样就得写n条类似select name from students where id = 1 的sql语句(因为student中有n个不同的id)
想跟大家请教下怎么样可以用一条sql解决这个问题。selectSQL

解决方案 »

  1.   

    顺序必须为lily tom john,必须要有排序字段,即order by xxx,否则每次顺序不稳定。
    另外,一句sql怎样作判断,太不明确了,最好举例说明想得到的结果。
      

  2.   

    谢谢楼上两位
    我有个想法是为students加一个新字段叫sequence
    id name sequence
    1  lily 0
    1  tom  1
    1  john 2
    2  lily 0
    2  tom  1
    2  john 2
    ... ...
    ... ...
    n  lily 0
    n  tom  1
    n  john 2类似这样,然后可以判断sequence=0的,必须对应lily,sequence=1 对应tom sequence为2的对应john。
    但是我该怎么能把我原来的表加上这么一列呢,请大家赐教啊。
    加上后,我可以这么写sql就能找出错误的了。 select * from students where sequence =0 and name!=lily. 
      

  3.   

    没具体明白zhaoxiangchong 的意思
      

  4.   

    由于现有数据无序,估计很难准确实现。
    或者假定目前都正确,即sequence=0的,必须对应lily,sequence=1 对应tom sequence为2的对应john
    或者先按照rownum或rowid新增顺序列,然后处理成0 1 2
      

  5.   


    你看到数据的顺序,与数据库保存数据的顺序必须是不一致的,所以,必须有一个辅助字段可以作为排序的依据.可以使用WMSYS.WM_CONCAT函数来形成结果集,然后使用lily tom john 进行筛选.WMSYS.WM_CONCAT(name) = 'lily,tom,john'