Using "Case When [then]" in [Order by] can make it success Example: SELECT * FROM [TableName] where [Column] in ('A','B','C','D') Order by CASE [Column] WHEN 'A' THEN 1 WHEN 'B' THEN 2 WHEN 'C' THEN 3 WHEN 'D' THEN 4 end
有時候要隨機找出table中的幾個數值 就可以用隨機排序的方法來處理 在SQL中 可以這樣用 select [ID] from [table] order by NewID() 隨機排序 select top N [ID] from [table] order by NewID() 隨機抓取N筆資料 在Linq上可以這樣 v=(From p In [Table] Select p.ID) .ToList().OrderBy(Function() Guid.NewGuid).ToList() 隨機排序 v=(From p In [Table] Select p.ID) .ToList().OrderBy(Function() Guid.NewGuid).Take(n).ToList() 隨機抓取N筆資料 不過聽說Linq有以下的用法 可是我自己測試都不成功 但是大家還是可以測一下 var result = (from r in [Table] order by Guid.NewGuid() select r).Take(3) -- MySQL: SELECT column FROM table ORDER BY RAND() LIMIT 1 in PostgreSQL: SELECT column FROM table ORDER BY RANDOM() LIMIT 1 in IBM DB2 SELECT column, RAND() as IDX FROM table ORDER BY IDX FETCH FIRST 1 ROWS ONLY in Oracle: SELECT column FROM ( SELECT column FROM table ORDER BY dbms_random.value ) WHERE rownum = 1