MySQL选择对同一个表

时间:2016-05-15 08:30:46

标签: php mysql multiple-select

我试图创建一个查询来从同一个表中选择多个行,将它们分组为一个数组。 现在我用这样的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");

----

我想在一个查询中合并它们,所以我得到一个关联数组。 像这样:

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

所以tks包含所有5行,top_ths包含1行。 可能吗 ?感谢。

1 个答案:

答案 0 :(得分:0)

从组行中得到的结果是,制作附加字段

(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)