doujia4041 2017-04-12 06:25
浏览 39
已采纳

结合两个mysql组查询

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.

I have an identifier in my database day2_semifinal or day2_additional which basically identifies the type of semifinal.

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:

$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. After that query, i'll again, iterate over the day2_additional_nosemi groups and query the players inside of them:

$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). 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?

  • 写回答

1条回答 默认 最新

  • dtncv04228 2017-04-12 12:42
    关注

    I'm answering my own question as i figured out a way to do this. What i did, was removed ORDER BYfrom both queries and created a new query which concatenated the two with 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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。