问一条sql语句

我要查询所有记录,并且要让state=2的记录排在前面,我该怎么样表达?谢谢
[b]问题补充:[/b]
state 有可能是0,1,2,3,4,5,6。。。。
[b]问题补充:[/b]
又,如果用 Order.asc("XXX")这么表达?
[b]问题补充:[/b]
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(TableName.class);
detachedCriteria.addOrder(Order.asc("state"));
如果要用上面的方法表达,该怎么写?
[b]问题补充:[/b]
select * from table order by (case state when 2 then 1 else 2 end) asc

select * from table where state=2
union all
select * from table where state<>2

这2中方法哪种效率更高一些呢?
我本来的确想用hibernate的Criteria.,看来只能用sql了

你的需求只是把state=2的排在最前面,现在不是已经做到了么?
select * from table order by (case state when 2 then 1 else 2 end) asc
这个就是的啊,不管你state有多少个不同的值,这个SQL都OK啊。
Order.asc("XXX")什么意思?

问题有些模糊。
能不能根据state 排序?

select * from tablename order by case state when '2' then 1 end

楼上方法确实可行~

select * from table order by (case state when 2 then 1 else 2 end) asc

额 :cry: 晕死

你这问题问的。。。

select * from table where state=2
union
select * from table where state<>2

楼主是想用hibernate的Criteria.

引用grandboy兄的
select * from table where state=2
union
select * from table where state<>2
楼主用SQLQuery查询不行吗?
SQLQuery query=session.createSQLQuery(sql语句);

用子查询,先查出state=2的这一条数据,再查询除state=2的其他数据,最后 把这2个查询出来的虚拟表统一查询就是了

select * from table where state=2
union [color=red]all[/color]
select * from table where state<>2
应该用union all,如果用union会把两个语句的综合结果排序其结果跟select * from table没差别,union all不排序,他会把第一个sql的结果放在前面,后一个sql的结果紧随其后