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

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

我有一个mysql表:

  1. Game | Type
  2. -------|----------
  3. Game1 | Action
  4. Game2 | Roleplay
  5. Game3 | Action
  6. Game4 | Sandbox
  7. Game5 | Sandbox
  8. Game6 | Roleplay
  9. Game7 | Fighting
  10. Game8 | Fighting
  11. Game9 | Adventure
  12. Game10 | Adventure
  13. Game11 | Action
  14. Game12 | Roleplay
  15. Game13 | Action
  16. Game14 | Sandbox
  17. Game15 | Sandbox
  18. Game16 | Roleplay
  19. Game17 | Fighting
  20. Game18 | Fighting
  21. Game19 | Adventure
  22. Game20 | Adventure
  23. Game21 | Roleplay
  24. Game23 | Action
  25. Game24 | Sandbox
  26. Game25 | Sandbox

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

  1. Game1 | Action
  2. -------|----------
  3. Game3 | Action
  4. Game7 | Action
  5. Game11 | Action
  6. Game13 | Action
  7. Game2 | Roleplay
  8. Game6 | Roleplay
  9. Game9 | Roleplay
  10. Game12 | Roleplay
  11. Game19 | Roleplay
  12. Game7 | Fighting
  13. Game8 | Fighting
  14. Game17 | Fighting
  15. Game18 | Fighting
  16. Game18 | Fighting
  17. Game9 | Adventure
  18. Game10 | Adventure
  19. Game19 | Adventure
  20. Game20 | Adventure
  21. Game20 | Adventure
  22. Game4 | Sandbox
  23. Game5 | Sandbox
  24. Game14 | Sandbox
  25. Game15 | Sandbox
  26. Game15 | Sandbox

最好使用1个sql和 mysqli_query .

ilmyapht

ilmyapht1#

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

两个问题。

相关问题