SQLite选择/过滤表并限制输出

时间:2015-11-24 20:01:35

标签: sql database sqlite

我正在为学校做一个SQL项目我有这个表:

 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

一起使用后发送的代码

2 个答案:

答案 0 :(得分:0)

如果你知道你想要的5个角色,你可以对5个子查询使用限制,然后将它们合并在一起:

select name, role from mytable where role = 'Support' limit 5
union all
select name, role from mytable where role = 'Assassin' limit 5
union all
select name, role from mytable where role = 'Warrior' limit 5
union all
select name, role from mytable where role = 'Specialist' limit 5
union all
select name, role from mytable where role = 'Mom' limit 5

SQL在随机行选择方面并不擅长,但如果你有行ID,你可以使用rand()(如果你使用的是mysql)并使用它来选择随机行id。你提到和我使用的限制5可能每次返回一组非常相似的行。

答案 1 :(得分:0)

考虑为每个不同的角色使用5个随机名称的联合查询。由于UNION查询不支持多个LIMITORDER BY子句,因此使用表别名。偏移量用于列出不同角色列表。

SELECT * FROM 
   (SELECT g.name, g.role 
    FROM games g
    WHERE g.role = 'Specialist'
    ORDER BY RANDOM() LIMIT 5) As t1

UNION

SELECT * FROM 
   (SELECT g.name, g.role 
    FROM games g
    WHERE g.role = 'Support'
    ORDER BY RANDOM() LIMIT 5) As t2

UNION

SELECT * FROM 
   (SELECT g.name, g.role 
    FROM games g
    WHERE g.role = 'Warrior'
    ORDER BY RANDOM() LIMIT 5) As t3

UNION

SELECT * FROM 
   (SELECT g.name, g.role 
    FROM games g
    WHERE g.role = 'Assassin'
    ORDER BY RANDOM() LIMIT 5) As t4

UNION

SELECT * FROM 
   (SELECT g.name, g.role 
    FROM games g
    WHERE g.role = 'Admin'
    ORDER BY RANDOM() LIMIT 5) As t5;