如何在表中获得每种类型的5个

z9smfwbn  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(287)

我有一个mysql表:

Game   |  Type  
-------|----------
Game1  | Action  
Game2  | Roleplay  
Game3  | Action  
Game4  | Sandbox  
Game5  | Sandbox  
Game6  | Roleplay  
Game7  | Fighting  
Game8  | Fighting  
Game9  | Adventure  
Game10 | Adventure 
Game11 | Action  
Game12 | Roleplay  
Game13 | Action  
Game14 | Sandbox  
Game15 | Sandbox  
Game16 | Roleplay  
Game17 | Fighting  
Game18 | Fighting  
Game19 | Adventure  
Game20 | Adventure
Game21 | Roleplay  
Game23 | Action  
Game24 | Sandbox  
Game25 | Sandbox

我如何将它们分成5组,每组5个,如下所示:

Game1  | Action  
-------|----------
Game3  | Action  
Game7  | Action  
Game11 | Action  
Game13 | Action  
Game2  | Roleplay  
Game6  | Roleplay  
Game9  | Roleplay   
Game12 | Roleplay  
Game19 | Roleplay    
Game7  | Fighting
Game8  | Fighting  
Game17 | Fighting   
Game18 | Fighting  
Game18 | Fighting     
Game9  | Adventure  
Game10 | Adventure  
Game19 | Adventure   
Game20 | Adventure 
Game20 | Adventure    
Game4  | Sandbox 
Game5  | Sandbox  
Game14 | Sandbox   
Game15 | Sandbox  
Game15 | Sandbox

最好使用1个sql和 mysqli_query .

e0uiprwp

e0uiprwp1#

$query_a = "SELECT DISTINCT Action FROM table";
$result_a = mysqli_query($connection, $query_a) or die(mysqli_error($connection));
while($row_a = mysqli_fetch_assoc($result_a) { 
    $query_g = "SELECT * FROM table WHERE Action = ".$row_a['Action']." LIMIT 5";
    $result_g = mysqli_query($connection, $query_g) or die(mysqli_error($connection));
    while($row_g = mysqli_fetch_assoc($result_g) { 
        echo $row_g['Game'].' '.row_g['Action'].'<br>';
    } result_g -> close();  
}$result_a -> close();

两个问题。

相关问题