如下表结构,要求 当 property = a 时,只拿出第一条记录,当property为其他值时,拿所有的记录
id   property 
---------------
1      a
2      b
3      c
4      b
5      a
6      a
7      b
8      a
9      c搜索结果:
id     property
------------------
1         a
2         b
3         c
4         b
7         b
9         c
谁能帮帮我啊,这个sql语句实在是解决不了。
最终如果能写成hql,更加感激!

解决方案 »

  1.   


    select * from table where property = a limit 1 union select * from table where propery != a将table替换为你的表名,不知道你要的是不是这个意思。
      

  2.   

    考虑到这个需求确实十分怪异,也许你把它写成两条SQL,然后union应该更简单些。
      

  3.   

    用的是mysql还是oracle用union就可以解决的。
    先把=a的第一条取出来,然后再把<>a的所有记录取出来,
    两者合并
      

  4.   

    一定要用SQL实现么,用程序应该更简单些~
      

  5.   

    sqlserver: select top 1 * from table_name where property='a'
                  union select * from table_name where property <> 'a'oracle: select * from table_name where property='a' and rownum=1
                  union select * from table_name where property <> 'a'
      

  6.   

      select top 1 * from table_name where property='a'
      union select * from table_name where property <> 'a'