简体   繁体   English

MySQL从同一表中选择对多行分组

[英]MySQL select grouping multiple rows from same table

I'm trying to create a query to select multiple rows from the same table grouping them like an array. 我正在尝试创建一个查询,以从同一张表中选择多行,将它们像一个数组一样分组。 Now i'm selecting them using php like this: 现在,我使用php这样选择它们:

$tks = mysqli_query($con,"SELECT * FROM hof ORDER BY tks DESC LIMIT 5");

$top_ths = mysqli_query($con,"SELECT * FROM hof ORDER BY ths DESC LIMIT 1");

$top_tha = mysqli_query($con,"SELECT * FROM hof ORDER BY tha DESC LIMIT 1");

----

I would like to merge them in a single query so i get an associative array. 我想将它们合并在一个查询中,以便得到一个关联数组。 Something like this: 像这样:

(SELECT * FROM hol ORDER BY tks DESC LIMIT 5) AS tks
UNION
(SELECT * FROM hol ORDER BY ths DESC LIMIT 1) AS top_ths

So tks contains all the 5 rows and top_ths contains 1 row. 因此tks包含所有5行,而top_ths包含1行。 Is it possible ? 可能吗 ? Thanks. 谢谢。

to undersatnd from wich group row is, make additional field 从最下面的组行开始,添加其他字段

(SELECT *, 1 as `group` FROM hol ORDER BY tks DESC LIMIT 5)
UNION
(SELECT *, 2 as `group` FROM hol ORDER BY ths DESC LIMIT 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM