简体   繁体   中英

php mysqli query select

I have 3 different tables table 1 keep general scores over the year table 2 and 3 is event specific only common field in all 3 is a member ID

I need to select 5 top score results for each member from table1 combine them with 1 specific result from table 2 and 3 I managed to get everything together in a temp table, but cnt get the output the way I need it example what i have and need.

在此处输入图片说明

Original code $prevQuery = "SELECT distinct member_id FROM scores"; $prevResult = $conn->query($prevQuery); while($row = $prevResult->fetch_assoc()) { $scores=("SELECT member_id,event_id,event_date,event_score FROM scores where member_id = ".$id." ORDER BY event_score DESC LIMIT 5");

Query:

select * from temp_table order by mem_id asc

PHP:

you can simple do your expected result in your application like this

<?php   
$result = array(array('mem_id'=>1,'location'=>'A','date'=>'20/05/2017','score'=>100),array('mem_id'=>1,'location'=>'B','date'=>'21/05/2017','score'=>103),array('mem_id'=>1,'location'=>'C','date'=>'22/05/2017','score'=>106),array('mem_id'=>1,'location'=>'C','date'=>'23/05/2017','score'=>108),
                array('mem_id'=>2,'location'=>'A','date'=>'20/05/2017','score'=>105),array('mem_id'=>2,'location'=>'B','date'=>'21/05/2017','score'=>109),array('mem_id'=>2,'location'=>'C','date'=>'22/05/2017','score'=>111),array('mem_id'=>2,'location'=>'C','date'=>'23/05/2017','score'=>110));

$new_result=array();
foreach($result as $key=>$row)
{

    $new_result[$row['mem_id']][]=$row;
}

echo "<table border='1px'>";
echo "<thead><tr><th>S.No</th><th>date</th><th>score1</th><th>date</th><th>score2</th><th>date</th><th>score3</th><th>date</th><th>score4</th></tr>";
echo "<tbody>";
foreach($new_result as $key=>$row)
{

    echo "<tr><td>".$key."</td>";
    foreach($row as $key1=>$row1)
    {
        echo "<td>".$row1['date']."</td>";
        echo "<td>".$row1['score']."</td>";
    }
    echo "</tr>";
}
echo "</tbody>"; 
echo "</table>";
?>

OUTPUT :

S.No    date    score1  date    score2  date    score3  date    score4
1   20/05/2017  100 21/05/2017  103 22/05/2017  106 23/05/2017  108
2   20/05/2017  105 21/05/2017  109 22/05/2017  111 23/05/2017  110

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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