以 Oracle 為例:
<pre class="c" name="code">
DROP TABLE lottery;
DROP TABLE drawn_numbers;
DROP TABLE ticket;
CREATE TABLE lottery (numbers INTEGER);
CREATE TABLE ticket (numbers INTEGER);
CREATE TABLE drawn_numbers (numbers INTEGER);
-- 投入號碼球
INSERT INTO lottery
SELECT ROWNUM
FROM all_objects
WHERE ROWNUM <= 39;
COMMIT;
-- 自選號
INSERT INTO ticket VALUES (2);
--INSERT INTO ticket VALUES (3);
--INSERT INTO ticket VALUES (5);
INSERT INTO ticket VALUES (7);
--INSERT INTO ticket VALUES (11);
INSERT INTO ticket VALUES (13);
--INSERT INTO ticket VALUES (17);
--INSERT INTO ticket VALUES (19);
INSERT INTO ticket VALUES (23);
--INSERT INTO ticket VALUES (29);
--INSERT INTO ticket VALUES (31);
INSERT INTO ticket VALUES (37);
COMMIT;
-- 開彩
-- 電腦隨機選號
INSERT INTO drawn_numbers
SELECT x.numbers
FROM ( SELECT numbers
FROM lottery
ORDER BY DBMS_RANDOM.VALUE) x
WHERE ROWNUM <= 5;
COMMIT;
-- 派彩
SELECT CASE x.criteria
WHEN 5 THEN '特獎'
WHEN 4 THEN '一獎'
WHEN 3 THEN '二獎'
WHEN 2 THEN '三獎'
ELSE '沒獎'
END
AS prize
FROM (SELECT COUNT (*) AS criteria
FROM drawn_numbers dn INNER JOIN ticket t ON dn.numbers = t.numbers) x;
這種邏輯應該用其它程式寫吧....用SQL不太適合
用C#就簡單多了