[英]Combine two mysql group queries
I'm making a web app to create tournaments and as i have learned PHP in the course of this project, so my skills aren't probably the best. 我正在制作一个Web应用程序来创建锦标赛,并且由于我在该项目的过程中学习了PHP,所以我的技能可能不是最好的。
I have an identifier in my database day2_semifinal
or day2_additional
which basically identifies the type of semifinal. 我的数据库
day2_semifinal
或day2_additional
有一个标识符,它基本上标识半决赛的类型。
So my first query is: 所以我的第一个查询是:
$numberquery = mysql_query("
SELECT *
FROM tourneyplayers
INNER JOIN results
on (resultid=r_id)
INNER JOIN players
ON (p_id=playerid)
INNER JOIN tourneys
on (T_Id=tourneyid)
WHERE tourneyid='$tourneyid' and
in_day2 = 1 and
day2_semifinal IS NOT NULL
GROUP BY day2_semifinal
ORDER BY agegroupid",$connection);
This will get me all the semifinal groups, i'll iterate over them and query all the players in group: 这将使我进入所有准决赛小组,我将遍历它们并查询小组中的所有球员:
$semigroup = $group['day2_semifinal'];
$playerQuery = mysql_query("
SELECT *
FROM tourneyplayers
INNER JOIN results
on (r_id=resultid)
INNER JOIN players
on (p_id=playerid)
WHERE tourneyid='$tourneyid' AND
day2_semifinal = '$semigroup' and
in_day2 = 1
ORDER BY day2startplace",$connection);
Now after i've created tables and echoed all the data from player queries for day2_semifinal
, i run another query: 现在,在创建表并回显了
day2_semifinal
玩家查询中的所有数据day2_semifinal
,我运行另一个查询:
$numberquery = mysql_query("SELECT * FROM tourneyplayers INNER JOIN results on (resultid=r_id) INNER JOIN players ON (p_id=playerid) WHERE tourneyid='$tourneyid' and in_day2 = 1 and day2_additional_nosemi IS NOT NULL AND day2_additional_nosemi <> 0 GROUP BY day2_additional_nosemi ORDER BY agegroupid",$connection);
Which is fairly similar to the first one, only thing different is day2_semifinal
identifiers have changed to day2_additional
. 这与第一个非常相似,唯一不同的是
day2_semifinal
标识符已更改为day2_additional
。 After that query, i'll again, iterate over the day2_additional_nosemi
groups and query the players inside of them: 查询之后,我将再次遍历
day2_additional_nosemi
组并查询其中的玩家:
$additionalgroup = $group['day2_additional_nosemi'];
$playerQuery = mysql_query("SELECT * FROM tourneyplayers INNER JOIN results on (r_id=resultid) INNER JOIN players on (p_id=playerid) WHERE tourneyid='$tourneyid' AND day2_additional_nosemi = '$additionalgroup' and in_day2 = 1 ORDER BY day2startplace",$connection);
Now this works, but this creates an issue with ordering, since the first query orders them by agegroupid
but only for players in day2_semifinal
(and i'd like to have day2_additional
players ordered together with day2_semifinal
). 现在这行得通,但是这会造成排序问题,因为第一个查询按
agegroupid
对其进行agegroupid
但仅针对day2_semifinal
玩家(我希望与day2_semifinal
一起订购day2_additional
玩家)。 If i run another query the previous data has already been echoed and ordering is not right. 如果我运行另一个查询,先前的数据已经被回显,并且排序不正确。 How could i concatenate two
$numberquery
queries in order to select players after them as well? 我如何连接两个
$numberquery
查询,以便在他们之后也选择玩家?
I'm answering my own question as i figured out a way to do this. 我在想出一种解决方法时回答了自己的问题。 What i did, was removed
ORDER BY
from both queries and created a new query which concatenated the two with UNION
: 我所做的是,从两个查询中删除了
ORDER BY
,并创建了一个新查询,将两个查询与UNION
连接起来:
SELECT * FROM (
SELECT *
FROM tourneyplayers as tp1
INNER JOIN results as r1
on (tp1.resultid=r1.r_id)
INNER JOIN players as p1
ON (p1.p_id=tp1.playerid)
WHERE tp1.tourneyid=96 and
tp1.in_day2 = 1 and
r1.day2_semifinal IS NOT NULL
GROUP BY r1.day2_semifinal
UNION ALL
SELECT *
FROM tourneyplayers as tp2
INNER JOIN results as r2
on (tp2.resultid=r2.r_id)
INNER JOIN players as p2
ON (p2.p_id=tp2.playerid)
WHERE tp2.tourneyid=96 and
tp2.in_day2 = 1 and
r2.day2_additional_nosemi IS NOT NULL AND
r2.day2_additional_nosemi <> 0
GROUP BY r2.day2_additional_nosemi
) t ORDER BY t.agegroupid;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.