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();
精彩评论