Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MySQL事件联盟加入3个表_Php_Mysql - Fatal编程技术网

Php MySQL事件联盟加入3个表

Php MySQL事件联盟加入3个表,php,mysql,Php,Mysql,我有桌子: 事件 -----+-------------------+ | id | eventName | -----+-------------------+ | 1 | Bowling | | 2 | Quiz | | 3 | Darts | | 4 | Pool | | 5 | Treasure Hunt

我有桌子:

事件

-----+-------------------+ | id | eventName | -----+-------------------+ | 1 | Bowling | | 2 | Quiz | | 3 | Darts | | 4 | Pool | | 5 | Treasure Hunt | | 6 | Snooker | -----+-------------------+ -----+-------------------+ |id |事件名称| -----+-------------------+ |1 |保龄球| |2 |测验| |3 |省道| |4 |游泳池| |5 |寻宝| |6 |斯诺克| -----+-------------------+ 竞争者

------+------------------+ | id | competitorName | ------+------------------+ | 1 | Microsoft | | 2 | BBC | | 3 | Facebook | ------+------------------+ ------+------------------+ |id |竞争对手名称| ------+------------------+ |1 |微软| |2 |英国广播公司| |3 | Facebook| ------+------------------+ 结果

------+---------------+---------+-------------+ | id | competitorId | eventId | eventScore | ------+---------------+---------+-------------+ | 1 | 1 | 1 | 12 | | 2 | 1 | 2 | 11 | | 3 | 1 | 3 | 23 | | 4 | 2 | 1 | 66 | | 5 | 2 | 2 | 12 | | 6 | 2 | 3 | 11 | | 7 | 2 | 4 | 23 | | 8 | 2 | 5 | 66 | | 3 | 2 | 6 | 23 | | 4 | 3 | 1 | 66 | | 5 | 3 | 2 | 12 | | 6 | 3 | 3 | 11 | | 7 | 3 | 4 | 23 | | 8 | 3 | 6 | 66 | ------+---------------+---------+-------------+ ------+---------------+---------+-------------+ |id | competitorId | eventId | eventScore| ------+---------------+---------+-------------+ | 1 | 1 | 1 | 12 | | 2 | 1 | 2 | 11 | | 3 | 1 | 3 | 23 | | 4 | 2 | 1 | 66 | | 5 | 2 | 2 | 12 | | 6 | 2 | 3 | 11 | | 7 | 2 | 4 | 23 | | 8 | 2 | 5 | 66 | | 3 | 2 | 6 | 23 | | 4 | 3 | 1 | 66 | | 5 | 3 | 2 | 12 | | 6 | 3 | 3 | 11 | | 7 | 3 | 4 | 23 | | 8 | 3 | 6 | 66 | ------+---------------+---------+-------------+ 我希望从中获得如下输出:

--------------+---------+---------+--------+------+--------------+---------- | competitor | Bowling | Quiz | Darts |Pool |Treasure Hunt | Snooker | --------------+---------+---------+--------+------+--------------+---------- | Microsoft | 12 | 11 | 23 | 0 | 0 | 0 | | BBC | 66 | 12 | 11 | 23 | 66 | 23 | | Facebook | 66 | 12 | 11 | 23 | 0 | 66 | --------------+---------+---------+--------+------+--------------+----------
// we have $competitors array containing id => name
// we have $events array containing id => name
// we have $results array containing competitorId => [ eventId => score ]
echo '<table>';
echo '<tr><td>Competitors</td>';
foreach($events as $e)
    echo '<td>'.$e.'</td>';
echo '</tr>';
foreach($results as $cId => $val) {
    echo '<tr><td>'.$competitors[$cId].'</td>';
    foreach($events as $eId => $name) {
        echo '<td>'.$val[$eId].'</td>';
    }
    echo '</tr>';
}
echo '</table>';
--------------+---------+---------+--------+------+--------------+---------- |选手|保龄球|测验|飞镖|游泳池|寻宝|斯诺克| --------------+---------+---------+--------+------+--------------+---------- |微软| 12 | 11 | 23 | 0 | 0 | 0| |英国广播公司| 66 | 12 | 11 | 23 | 66 | 23| |Facebook | 66 | 12 | 11 | 23 | 0 | 66| --------------+---------+---------+--------+------+--------------+----------
我尝试过各种连接和嵌套php,但我就是无法破解它。我甚至不确定这是否可能。我的SQL知识不是很好,我尝试过一些教程,但它们没有涵盖这种查询。

您基本上是在寻找透视表。有一个示例和。

此请求通常正常,但每次在表事件中添加新条目时都需要修改。

SELECT
    comp.competitorName as competitor,
    rbowling.eventScore as Bowling,
    rquiz.eventScore as Quiz,
    rdarts.eventScore as Darts,
    rpool.eventScore as Pool,
    rtreasure.eventScore as Treasure_Hunt,
    rsnooker.eventScore as Snooker
FROM competitors comp
LEFT JOIN result rbowling ON (rbowling.eventId = 1 AND comp.id = rbowling.competitorId)
LEFT JOIN result rquiz ON (rquiz.eventId = 2 AND comp.id = rquiz.competitorId)
LEFT JOIN result rdarts ON (rdarts.eventId = 3 AND comp.id = rdarts.competitorId)
LEFT JOIN result rpool ON (rpool.eventId = 4 AND comp.id = rpool.competitorId)
LEFT JOIN result rtreasure ON (rtreasure.eventId = 5 AND comp.id = rtreasure.competitorId)
LEFT JOIN result rsnooker ON (rsnooker.eventId = 6 AND comp.id = rsnooker.competitorId)
GROUP BY comp.id;

您要查找的查询如下:

SELECT r.eventScore, e.eventName, c.competitorName
FROM results r
LEFT JOIN events e ON e.id = r.eventId
LEFT JOIN competitors c ON c.id = r.competitorId
通过此查询,您将获得每个结果的基本事件和竞争对手名称。 然后您必须在PHP中管理数据以创建所需的表。。。但我想这很难实现

我最好分别选择参赛者和赛事,然后获得所有得分结果,并创建如下表格:

--------------+---------+---------+--------+------+--------------+---------- | competitor | Bowling | Quiz | Darts |Pool |Treasure Hunt | Snooker | --------------+---------+---------+--------+------+--------------+---------- | Microsoft | 12 | 11 | 23 | 0 | 0 | 0 | | BBC | 66 | 12 | 11 | 23 | 66 | 23 | | Facebook | 66 | 12 | 11 | 23 | 0 | 66 | --------------+---------+---------+--------+------+--------------+----------
// we have $competitors array containing id => name
// we have $events array containing id => name
// we have $results array containing competitorId => [ eventId => score ]
echo '<table>';
echo '<tr><td>Competitors</td>';
foreach($events as $e)
    echo '<td>'.$e.'</td>';
echo '</tr>';
foreach($results as $cId => $val) {
    echo '<tr><td>'.$competitors[$cId].'</td>';
    foreach($events as $eId => $name) {
        echo '<td>'.$val[$eId].'</td>';
    }
    echo '</tr>';
}
echo '</table>';
//我们有$competitors数组,其中包含id=>name
//我们有$events数组,其中包含id=>name
//我们有$results数组,其中包含competitorId=>[eventId=>score]
回声';
呼应"竞争者",;
foreach($e形式的事件)
回音“.$e.”;
回声';
foreach($cId=>$val的结果){
回显“.$competitors[$cId]”;
foreach($eId=>$name形式的事件){
回显“.$val[$eId]”;
}
回声';
}
回声';

但这只是一个快速且不太好的解决方案,我最好只使用一个查询。

您可以像这样用php创建查询

<?php
$events = array('Assume this is array of events table with id,eventName as key');

$column = array();
foreach($events as $event)
{
     $column[] = 'SUM(IF(results.eventId = '.$event['id'].', results.eventScore, 0)) AS '.$event['eventName'];
}

$sql = 'SELECT   competitors.competitorName AS competitor,';
$sql .= implode(',',$column);
$sql .= ' FROM competitors';
$sql .= ' LEFT JOIN results ON results.competitorId = competitors.id';
$sql .= ' GROUP BY competitors.id';
//Put $sql into mysql_query then

比我的回答更优雅!感谢bitoshi-n,这几乎适用于我的目的,但我不能在不使用harcCoded值的情况下取消分数。我需要这些是动态的,因为事件可能会改变。而($row=mysql_fetch_array($result)){echo$row['competitor']。'.$row['Bowling']。$row['Darts'];echo“
”;}非常感谢。现在有意义了:)@user1156756。好的,那么你可以通过点击答案左边的复选框框框来选择任何被接受的答案。你知道我如何按顺序排列,以便在所有事件中得分最高的竞争对手行成为第一条返回的记录吗?@user1156756 add
SUM(results.eventScore)作为
中的sumScore
,选择
字段,以便您可以在查询中按put
按sumScore排序