我在做一個項目的SQL學校輸出我有這個表:SQLite的選擇/過濾表並限制
name price_gold price_euros role universe
---------- ---------- ----------- ---------- -----------------
Sylvanas 10,000 9,99 Specialist World of Warcraft
The Lost V 10,000 9,99 Specialist The Lost Vikings
Azmodan 10,000 9,99 Specialist Diablo
Zagara 7,000 8,49 Specialist Starcraft
Murky 7,000 8,49 Specialist World of Warcraft
Abathur 10,000 9,99 Specialist Starcraft
Gazlowe 4,000 6,49 Specialist World of Warcraft
Nazeebo 10,000 9,99 Specialist Diablo
Sgt. Hamme 7,000 8,49 Specialist Starcraft
Artanis 10,000 9,99 Warrior Starcraft
Rexxar 10,000 9,99 Warrior World of Warcraft
Leoric 10,000 9,99 Warrior Diablo
Johanna 10,000 9,99 Warrior Diablo
Anub'arak 4,000 6,49 Warrior World of Warcraft
Chen 7,000 8,49 Warrior World of Warcraft
Arthas 7,000 8,49 Warrior World of Warcraft
Diablo 7,000 8,49 Warrior Diablo
E.T.C. 2,000 3,99 Warrior World of Warcraft
Muradin 2,000 3,99 Warrior World of Warcraft
Sonya 4,000 6,49 Warrior Diablo
Stitches 7,000 8,49 Warrior World of Warcraft
Tyrael 4,000 6,49 Warrior Diablo
Cho Warrior World of Warcraft
Gall Assassin World of Warcraft
The Butche 10,000 9,99 Assassin Diablo
Kael'thas 10,000 9,99 Assassin World of Warcraft
Thrall 10,000 9,99 Assassin World of Warcraft
Jaina 4,000 6,49 Assassin World of Warcraft
Tychus 4,000 6,49 Assassin Starcraft
Falstad 7,000 8,49 Assassin World of Warcraft
Illidan 4,000 6,49 Assassin World of Warcraft
而且我想查詢一個選擇的5個不同的角色和5名隨機的名字,即這樣的:
name role
---------- ---------
Sylvanas Specialis
The Lost V Specialis
Azmodan Specialis
Zagara Specialis
Murky Specialis
Johanna Warrior
Anub'arak Warrior
Chen Warrior
Arthas Warrior
Stitches Warrior
The Butche Assassin
Kael'thas Assassin
Thrall Assassin
Jaina Assassin
Tychus Assassin
Lt. Morale Support
Kharazim Support
Reghar Support
Brightwing Support
我嘗試使用Limit
但只給出了表的前5個要素。
怎麼辦?
EDIT1:
SELECT * FROM
(SELECT hero.name, hero.role
FROM hero
INNER JOIN
(SELECT DISTINCT role FROM hero
LIMIT 1 OFFSET 1) n
ORDER BY RANDOM() LIMIT 5) As t1
UNION
SELECT * FROM
(SELECT hero.name, hero.role
FROM hero
INNER JOIN
(SELECT DISTINCT role FROM hero
LIMIT 1 OFFSET 2) n
ORDER BY RANDOM() LIMIT 5) As t2
UNION
SELECT * FROM
(SELECT hero.name, hero.role
FROM hero
INNER JOIN
(SELECT DISTINCT role FROM hero
LIMIT 1 OFFSET 3) n
ORDER BY RANDOM() LIMIT 5) As t3
UNION
SELECT * FROM
(SELECT hero.name, hero.role
FROM hero
INNER JOIN
(SELECT DISTINCT role FROM hero
LIMIT 1 OFFSET 4) n
ORDER BY RANDOM() LIMIT 5) As t4
UNION
SELECT * FROM
(SELECT hero.name, hero.role
FROM hero
INNER JOIN
(SELECT DISTINCT role FROM hero
LIMIT 1 OFFSET 5) n
ORDER BY RANDOM() LIMIT 5) As t5
這是後我改變了它與我的表@Parfait
謝謝!這肯定會幫助 –
,但並不完美的伎倆sqlite似乎並不喜歡在子查詢結束時的「限制5」,並說:「錯誤:LIMIT子句應該在UNION ALL之前沒有之前'I將保持在它仍然感謝couse –
啊哈,我使用MySQL語法,它可以讓你在那裏使用限制。我猜你需要用子查詢來做一些奇特的步法,如果SQLite有它們,可能還會伴隨一個rand和row number。 – argoc