用SQL實現撲克牌排序

lt發表於2012-11-29
with t as(select mod(level-1,13)+1 l,trunc((level-1)/13)+1 f from dual connect by level<=52), --一副牌 l點數 f 花色
t1 as(select rownum rn,l,f from(select l,f from t order by dbms_random.random)), --打亂,rn序號
t2 as(select rownum rn,l,f from(select 
l,f
from t1 order by f,l)) --2到A排序
select t1.rn 序號,case t1.f when 1 then '黑桃'
when 2 then '紅桃'
when 3 then '梅花'
when 4 then '方片'
end  原花色,
t1.l 原點數,case t2.f when 1 then '黑桃'
when 2 then '紅桃'
when 3 then '梅花'
when 4 then '方片'
end 排序後花色,t2.l 排序後點數 
from t1,t2 where t1.rn=t2.rn order by t1.rn --優勢,利用現成的SQL排序功能
; 

相關文章