开发者

Select all and give random output (Random sorted output)

开发者 https://www.devze.com 2023-03-05 04:50 出处:网络
I have a table looking something like this ID Name City ------------- 0a开发者_如何转开发sdsda 1hrsgsh

I have a table looking something like this

ID Name City 
-------------
0  a开发者_如何转开发sd  sda
1  hrs  gsh
2  ghd  0
3  hsa  0
.
.

How could I return city != '0' in random order and then city = '0' in random order?


For SQL Server (RAND gives same value for all rows in SQL Server)

ORDER BY
    CASE WHEN City <> '0' THEN 1 ELSE 2 END,
    NEWID()


Assuming your DBMS supports a RAND function that returns a different random number for each row in your result set:

SELECT ID, Name, City
  FROM SomethingLikeThis
 ORDER BY CASE WHEN City = 0 THEN 1 ELSE 0 END, RAND();
0

精彩评论

暂无评论...
验证码 换一张
取 消