在PHP / MySql中对来自不同查询的表数据进行排序

时间:2011-03-29 23:28:59

标签: php mysql sorting

我真的很困惑,因为我是一个(很差)自学成才的业余爱好者。我正在建立一个联盟的统计网站,我设法创建一个表,从数据库中的几个表中获取数据并将它整理在一起。 表中的当前“列”是驾驶员姓名,比赛总数,总胜利和领奖台位置。驱动程序名称是我唯一的“简单”数据集 - 所有其余数据都是通过计算获得的,因此需要有关名称的初始数据。

目前,我的表格以驱动程序的字母顺序显示正确的数据。我该怎么做才能在其他三个字段中对它进行排序(即使在完全不同的页面中)?

目前的表格可以在这里看到:http://www.gpvwc.com/en/services/database/driver/

以下是我编写的代码:

$data90=mysql_query("SELECT DISTINCT competition FROM DCO_results ORDER BY competition ASC", $CONNECTW);
        while($row=mysql_fetch_row($data90)) {

        $competitionnum="$row[0]";

        $data80=mysql_query("SELECT competition FROM DCO_competitions WHERE id='$competitionnum'", $CONNECTW);
        while($row=mysql_fetch_row($data80)) {

        $compstring="$row[0]";  

            echo "<span class='footer'><a href='#$compstring'>$compstring</a> | </span>";
        }}


echo "<p>&nbsp;</p>";

$data70=mysql_query("SELECT DISTINCT competition FROM DCO_results ORDER BY competition ASC", $CONNECTW);
        while($row=mysql_fetch_row($data70)) {

        $compnum3="$row[0]";


$data60=mysql_query("SELECT competition FROM DCO_competitions WHERE id='$compnum3'", $CONNECTW);
        while($row=mysql_fetch_row($data60)) {

        $compstring3="$row[0]";         

echo "<a name='$compstring3' id='$compstring3'></a><h2>$compstring3</h2>";          

if ($compnum3=="1") {

$sql = '(SELECT DISTINCT driver1 FROM DCO_results WHERE competition="1"  AND driver1 IS NOT NULL) UNION (SELECT DISTINCT driver2 FROM DCO_results WHERE competition="1"  AND driver2 IS NOT NULL) UNION (SELECT DISTINCT driver3 FROM DCO_results WHERE competition="1"  AND driver3 IS NOT NULL) UNION (SELECT DISTINCT driver4 FROM DCO_results WHERE competition="1"  AND driver4 IS NOT NULL) UNION (SELECT DISTINCT driver5 FROM DCO_results WHERE competition="1"  AND driver5 IS NOT NULL) UNION (SELECT DISTINCT driver6 FROM DCO_results WHERE competition="1"  AND driver6 IS NOT NULL) UNION (SELECT DISTINCT driver7 FROM DCO_results WHERE competition="1"  AND driver7 IS NOT NULL) UNION (SELECT DISTINCT driver8 FROM DCO_results WHERE competition="1"  AND driver8 IS NOT NULL) UNION (SELECT DISTINCT driver9 FROM DCO_results WHERE competition="1"  AND driver9 IS NOT NULL) UNION (SELECT DISTINCT driver10 FROM DCO_results WHERE competition="1"  AND driver10 IS NOT NULL) UNION (SELECT DISTINCT driver11 FROM DCO_results WHERE competition="1"  AND driver11 IS NOT NULL) UNION (SELECT DISTINCT driver12 FROM DCO_results WHERE competition="1"  AND driver12 IS NOT NULL) UNION (SELECT DISTINCT driver13 FROM DCO_results WHERE competition="1"  AND driver13 IS NOT NULL) UNION (SELECT DISTINCT driver14 FROM DCO_results WHERE competition="1"  AND driver14 IS NOT NULL) UNION (SELECT DISTINCT driver15 FROM DCO_results WHERE competition="1"  AND driver15 IS NOT NULL) UNION (SELECT DISTINCT driver16 FROM DCO_results WHERE competition="1"  AND driver16 IS NOT NULL) UNION (SELECT DISTINCT driver17 FROM DCO_results WHERE competition="1"  AND driver17 IS NOT NULL) UNION (SELECT DISTINCT driver18 FROM DCO_results WHERE competition="1"  AND driver18 IS NOT NULL) UNION (SELECT DISTINCT driver19 FROM DCO_results WHERE competition="1"  AND driver19 IS NOT NULL) UNION (SELECT DISTINCT driver20 FROM DCO_results WHERE competition="1"  AND driver20 IS NOT NULL) UNION (SELECT DISTINCT driver21 FROM DCO_results WHERE competition="1"  AND driver21 IS NOT NULL) UNION (SELECT DISTINCT driver22 FROM DCO_results WHERE competition="1"  AND driver22 IS NOT NULL) UNION (SELECT DISTINCT driver23 FROM DCO_results WHERE competition="1"  AND driver23 IS NOT NULL) UNION (SELECT DISTINCT driver24 FROM DCO_results WHERE competition="1"  AND driver24 IS NOT NULL) UNION (SELECT DISTINCT driver25 FROM DCO_results WHERE competition="1"  AND driver25 IS NOT NULL) UNION (SELECT DISTINCT driver26 FROM DCO_results WHERE competition="1"  AND driver26 IS NOT NULL) UNION (SELECT DISTINCT driver27 FROM DCO_results WHERE competition="1"  AND driver27 IS NOT NULL) UNION (SELECT DISTINCT driver28 FROM DCO_results WHERE competition="1"  AND driver28 IS NOT NULL) UNION (SELECT DISTINCT driver29 FROM DCO_results WHERE competition="1"  AND driver29 IS NOT NULL) UNION (SELECT DISTINCT driver30 FROM DCO_results WHERE competition="1"  AND driver30 IS NOT NULL) UNION (SELECT DISTINCT driver31 FROM DCO_results WHERE competition="1"  AND driver31 IS NOT NULL) UNION (SELECT DISTINCT driver32 FROM DCO_results WHERE competition="1"  AND driver32 IS NOT NULL) UNION (SELECT DISTINCT driver33 FROM DCO_results WHERE competition="1"  AND driver33 IS NOT NULL) UNION (SELECT DISTINCT driver34 FROM DCO_results WHERE competition="1"  AND driver34 IS NOT NULL) UNION (SELECT DISTINCT driver35 FROM DCO_results WHERE competition="1"  AND driver35 IS NOT NULL) UNION (SELECT DISTINCT driver36 FROM DCO_results WHERE competition="1"  AND driver36 IS NOT NULL) UNION (SELECT DISTINCT driver37 FROM DCO_results WHERE competition="1"  AND driver37 IS NOT NULL) UNION (SELECT DISTINCT driver38 FROM DCO_results WHERE competition="1"  AND driver38 IS NOT NULL) UNION (SELECT DISTINCT driver39 FROM DCO_results WHERE competition="1"  AND driver39 IS NOT NULL) UNION (SELECT DISTINCT driver40 FROM DCO_results WHERE competition="1"  AND driver40 IS NOT NULL) UNION (SELECT DISTINCT driver41 FROM DCO_results WHERE competition="1"  AND driver41 IS NOT NULL) UNION (SELECT DISTINCT driver42 FROM DCO_results WHERE competition="1"  AND driver42 IS NOT NULL) UNION (SELECT DISTINCT driver43 FROM DCO_results WHERE competition="1"  AND driver43 IS NOT NULL) UNION (SELECT DISTINCT driver44 FROM DCO_results WHERE competition="1"  AND driver44 IS NOT NULL) UNION (SELECT DISTINCT driver45 FROM DCO_results WHERE competition="1"  AND driver45 IS NOT NULL) UNION (SELECT DISTINCT driver46 FROM DCO_results WHERE competition="1"  AND driver46 IS NOT NULL) UNION (SELECT DISTINCT driver47 FROM DCO_results WHERE competition="1"  AND driver47 IS NOT NULL) UNION (SELECT DISTINCT driver48 FROM DCO_results WHERE competition="1"  AND driver48 IS NOT NULL) ORDER BY driver1 ASC';  

$result = mysql_query($sql); 

$totaldrivers=mysql_num_rows($result);

echo "<p>&nbsp;</p>";
echo "<p>A total of $totaldrivers drivers took part in the GPVWC $compstring3.</p>";

echo "<table width='100% border='0' cellspacing='0' cellpadding='0' align='left'>";

    echo "<tr>

                <th width='250' class='bg_th'><span class='footer'>Driver</span></th>
                <th width='50' class='bg_th' align='center'><span class='footer'><a href='$RKP/en/services/database/driver/sort/?target=Races'>Races</a></span></th>
                <th width='50' class='bg_th' align='center'><span class='footer'><a href='$RKP/en/services/database/driver/sort/?target=Wins'>Wins</a></span></th>
                <th width='50' class='bg_th' align='center'><span class='footer'><a href='$RKP/en/services/database/driver/sort/?target=Podiums'>Podiums</a></span></th>
                <th width='50' class='bg_th' align='center'><span class='footer'>Points</span></th>
                <th width='50' class='bg_th' align='center'><span class='footer'>Pts Finish</span></th>
            </tr>";

while($get_info = mysql_fetch_row($result)){

$driversnames="$get_info[0]";

$data55=mysql_query("SELECT id FROM DCO_results WHERE competition='$compnum3' AND session='R' AND (driver1='$driversnames' OR driver2='$driversnames' OR driver3='$driversnames' OR driver4='$driversnames' OR driver5='$driversnames' OR driver6='$driversnames' OR driver7='$driversnames' OR driver8='$driversnames' OR driver9='$driversnames' OR driver10='$driversnames' OR driver11='$driversnames' OR driver12='$driversnames' OR driver13='$driversnames' OR driver14='$driversnames' OR driver15='$driversnames' OR driver16='$driversnames' OR driver17='$driversnames' OR driver18='$driversnames' OR driver19='$driversnames'OR driver20='$driversnames' OR driver21='$driversnames' OR driver22='$driversnames' OR driver23='$driversnames' OR driver24='$driversnames' OR driver25='$driversnames' OR driver26='$driversnames' OR driver27='$driversnames' OR driver28='$driversnames' OR driver29='$driversnames' OR driver30='$driversnames' OR driver31='$driversnames' OR driver32='$driversnames' OR driver33='$driversnames' OR driver34='$driversnames' OR driver35='$driversnames' OR driver36='$driversnames' OR driver37='$driversnames' OR driver38='$driversnames' OR driver39='$driversnames' OR driver40='$driversnames' OR driver41='$driversnames' OR driver42='$driversnames' OR driver43='$driversnames' OR driver44='$driversnames' OR driver45='$driversnames' OR driver46='$driversnames' OR driver47='$driversnames' OR driver48='$driversnames') ORDER BY id DESC", $CONNECTW);
$totalraceslist=mysql_num_rows($data55);

$data66=mysql_query("SELECT id FROM DCO_results WHERE competition='$compnum3' AND session='R' AND (driver1='$driversnames') ORDER BY id DESC", $CONNECTW);
$totalwinslist=mysql_num_rows($data66);

$data77=mysql_query("SELECT id FROM DCO_results WHERE competition='$compnum3' AND session='R' AND (driver1='$driversnames' OR driver2='$driversnames' OR driver3='$driversnames') ORDER BY id DESC", $CONNECTW);
$totalpodiumslist=mysql_num_rows($data77);


    echo "<tr>

                <td width='250' class='bg_th'><span class='footer'><a href='$RKP/en/services/database/driver/?target=$driversnames'>$driversnames</a></span></td>
                <td width='50' class='bg_th' align='center'><span class='footer'>$totalraceslist</span></td>
                <td width='50' class='bg_th' align='center'><span class='footer'>$totalwinslist</span></td>
                <td width='50' class='bg_th' align='center'><span class='footer'>$totalpodiumslist</span></td>
                <td width='50' class='bg_th' align='center'><span class='footer'>N/A</span></td>
                <td width='50' class='bg_th' align='center'><span class='footer'>N/A</span></td>                    
            </tr>"; }}

if ($compnum3=="2") {

$sql = '(SELECT DISTINCT driver1 FROM DCO_results WHERE competition="2"  AND driver1 IS NOT NULL) UNION (SELECT DISTINCT driver2 FROM DCO_results WHERE competition="2"  AND driver2 IS NOT NULL) UNION (SELECT DISTINCT driver3 FROM DCO_results WHERE competition="2"  AND driver3 IS NOT NULL) UNION (SELECT DISTINCT driver4 FROM DCO_results WHERE competition="2"  AND driver4 IS NOT NULL) UNION (SELECT DISTINCT driver5 FROM DCO_results WHERE competition="2"  AND driver5 IS NOT NULL) UNION (SELECT DISTINCT driver6 FROM DCO_results WHERE competition="2"  AND driver6 IS NOT NULL) UNION (SELECT DISTINCT driver7 FROM DCO_results WHERE competition="2"  AND driver7 IS NOT NULL) UNION (SELECT DISTINCT driver8 FROM DCO_results WHERE competition="2"  AND driver8 IS NOT NULL) UNION (SELECT DISTINCT driver9 FROM DCO_results WHERE competition="2"  AND driver9 IS NOT NULL) UNION (SELECT DISTINCT driver10 FROM DCO_results WHERE competition="2"  AND driver10 IS NOT NULL) UNION (SELECT DISTINCT driver11 FROM DCO_results WHERE competition="2"  AND driver11 IS NOT NULL) UNION (SELECT DISTINCT driver12 FROM DCO_results WHERE competition="2"  AND driver12 IS NOT NULL) UNION (SELECT DISTINCT driver13 FROM DCO_results WHERE competition="2"  AND driver13 IS NOT NULL) UNION (SELECT DISTINCT driver14 FROM DCO_results WHERE competition="2"  AND driver14 IS NOT NULL) UNION (SELECT DISTINCT driver15 FROM DCO_results WHERE competition="2"  AND driver15 IS NOT NULL) UNION (SELECT DISTINCT driver16 FROM DCO_results WHERE competition="2"  AND driver16 IS NOT NULL) UNION (SELECT DISTINCT driver17 FROM DCO_results WHERE competition="2"  AND driver17 IS NOT NULL) UNION (SELECT DISTINCT driver18 FROM DCO_results WHERE competition="2"  AND driver18 IS NOT NULL) UNION (SELECT DISTINCT driver19 FROM DCO_results WHERE competition="2"  AND driver19 IS NOT NULL) UNION (SELECT DISTINCT driver20 FROM DCO_results WHERE competition="2"  AND driver20 IS NOT NULL) UNION (SELECT DISTINCT driver21 FROM DCO_results WHERE competition="2"  AND driver21 IS NOT NULL) UNION (SELECT DISTINCT driver22 FROM DCO_results WHERE competition="2"  AND driver22 IS NOT NULL) UNION (SELECT DISTINCT driver23 FROM DCO_results WHERE competition="2"  AND driver23 IS NOT NULL) UNION (SELECT DISTINCT driver24 FROM DCO_results WHERE competition="2"  AND driver24 IS NOT NULL) UNION (SELECT DISTINCT driver25 FROM DCO_results WHERE competition="2"  AND driver25 IS NOT NULL) UNION (SELECT DISTINCT driver26 FROM DCO_results WHERE competition="2"  AND driver26 IS NOT NULL) UNION (SELECT DISTINCT driver27 FROM DCO_results WHERE competition="2"  AND driver27 IS NOT NULL) UNION (SELECT DISTINCT driver28 FROM DCO_results WHERE competition="2"  AND driver28 IS NOT NULL) UNION (SELECT DISTINCT driver29 FROM DCO_results WHERE competition="2"  AND driver29 IS NOT NULL) UNION (SELECT DISTINCT driver30 FROM DCO_results WHERE competition="2"  AND driver30 IS NOT NULL) UNION (SELECT DISTINCT driver31 FROM DCO_results WHERE competition="2"  AND driver31 IS NOT NULL) UNION (SELECT DISTINCT driver32 FROM DCO_results WHERE competition="2"  AND driver32 IS NOT NULL) UNION (SELECT DISTINCT driver33 FROM DCO_results WHERE competition="2"  AND driver33 IS NOT NULL) UNION (SELECT DISTINCT driver34 FROM DCO_results WHERE competition="2"  AND driver34 IS NOT NULL) UNION (SELECT DISTINCT driver35 FROM DCO_results WHERE competition="2"  AND driver35 IS NOT NULL) UNION (SELECT DISTINCT driver36 FROM DCO_results WHERE competition="2"  AND driver36 IS NOT NULL) UNION (SELECT DISTINCT driver37 FROM DCO_results WHERE competition="2"  AND driver37 IS NOT NULL) UNION (SELECT DISTINCT driver38 FROM DCO_results WHERE competition="2"  AND driver38 IS NOT NULL) UNION (SELECT DISTINCT driver39 FROM DCO_results WHERE competition="2"  AND driver39 IS NOT NULL) UNION (SELECT DISTINCT driver40 FROM DCO_results WHERE competition="2"  AND driver40 IS NOT NULL) UNION (SELECT DISTINCT driver41 FROM DCO_results WHERE competition="2"  AND driver41 IS NOT NULL) UNION (SELECT DISTINCT driver42 FROM DCO_results WHERE competition="2"  AND driver42 IS NOT NULL) UNION (SELECT DISTINCT driver43 FROM DCO_results WHERE competition="2"  AND driver43 IS NOT NULL) UNION (SELECT DISTINCT driver44 FROM DCO_results WHERE competition="2"  AND driver44 IS NOT NULL) UNION (SELECT DISTINCT driver45 FROM DCO_results WHERE competition="2"  AND driver45 IS NOT NULL) UNION (SELECT DISTINCT driver46 FROM DCO_results WHERE competition="2"  AND driver46 IS NOT NULL) UNION (SELECT DISTINCT driver47 FROM DCO_results WHERE competition="2"  AND driver47 IS NOT NULL) UNION (SELECT DISTINCT driver48 FROM DCO_results WHERE competition="2"  AND driver48 IS NOT NULL) ORDER BY driver1 ASC';  

$result = mysql_query($sql); 

$totaldrivers=mysql_num_rows($result);

echo "<p>&nbsp;</p>";
echo "<p>A total of $totaldrivers drivers took part in the GPVWC $compstring3.</p>";

echo "<table width='100% border='0' cellspacing='0' cellpadding='0' align='left'>";

    echo "<tr>

                <th width='250' class='bg_th'><span class='footer'>Driver</span></th>
                <th width='50' class='bg_th' align='center'><span class='footer'>Races</span></th>
                <th width='50' class='bg_th' align='center'><span class='footer'>Wins</span></th>
                <th width='50' class='bg_th' align='center'><span class='footer'>Podiums</span></th>
                <th width='50' class='bg_th' align='center'><span class='footer'>Points</span></th>
                <th width='50' class='bg_th' align='center'><span class='footer'>Pts Finish</span></th>
            </tr>";

while($get_info = mysql_fetch_row($result)){

$driversnames="$get_info[0]";

$data55=mysql_query("SELECT id FROM DCO_results WHERE competition='$compnum3' AND session='R' AND (driver1='$driversnames' OR driver2='$driversnames' OR driver3='$driversnames' OR driver4='$driversnames' OR driver5='$driversnames' OR driver6='$driversnames' OR driver7='$driversnames' OR driver8='$driversnames' OR driver9='$driversnames' OR driver10='$driversnames' OR driver11='$driversnames' OR driver12='$driversnames' OR driver13='$driversnames' OR driver14='$driversnames' OR driver15='$driversnames' OR driver16='$driversnames' OR driver17='$driversnames' OR driver18='$driversnames' OR driver19='$driversnames'OR driver20='$driversnames' OR driver21='$driversnames' OR driver22='$driversnames' OR driver23='$driversnames' OR driver24='$driversnames' OR driver25='$driversnames' OR driver26='$driversnames' OR driver27='$driversnames' OR driver28='$driversnames' OR driver29='$driversnames' OR driver30='$driversnames' OR driver31='$driversnames' OR driver32='$driversnames' OR driver33='$driversnames' OR driver34='$driversnames' OR driver35='$driversnames' OR driver36='$driversnames' OR driver37='$driversnames' OR driver38='$driversnames' OR driver39='$driversnames' OR driver40='$driversnames' OR driver41='$driversnames' OR driver42='$driversnames' OR driver43='$driversnames' OR driver44='$driversnames' OR driver45='$driversnames' OR driver46='$driversnames' OR driver47='$driversnames' OR driver48='$driversnames') ORDER BY id DESC", $CONNECTW);
$totalraceslist=mysql_num_rows($data55);

$data66=mysql_query("SELECT id FROM DCO_results WHERE competition='$compnum3' AND session='R' AND (driver1='$driversnames') ORDER BY id DESC", $CONNECTW);
$totalwinslist=mysql_num_rows($data66);

$data77=mysql_query("SELECT id FROM DCO_results WHERE competition='$compnum3' AND session='R' AND (driver1='$driversnames' OR driver2='$driversnames' OR driver3='$driversnames') ORDER BY id DESC", $CONNECTW);
$totalpodiumslist=mysql_num_rows($data77);


    echo "<tr>

                <td width='250' class='bg_th'><span class='footer'><a href='$RKP/en/services/database/driver/?target=$driversnames'>$driversnames</a></span></td>
                <td width='50' class='bg_th' align='center'><span class='footer'>$totalraceslist</span></td>
                <td width='50' class='bg_th' align='center'><span class='footer'>$totalwinslist</span></td>
                <td width='50' class='bg_th' align='center'><span class='footer'>$totalpodiumslist</span></td>
                <td width='50' class='bg_th' align='center'><span class='footer'>N/A</span></td>
                <td width='50' class='bg_th' align='center'><span class='footer'>N/A</span></td>                    
            </tr>";     }}

    echo "</table>"; 
    echo "<p>&nbsp;</p>";}




    echo "</table>"; 
    echo "<p>&nbsp;</p>";   }   

我甚至不知道这是否可以理解(我可以说我作为一名编辑并不完全是规范的)但我已经失去了几天(和我的睡眠),我终于决定寻求帮助了。 / p>

2 个答案:

答案 0 :(得分:3)

欢迎来到复杂的网络技术世界:)

我无法真正为您提供完整的解决方案,但我会尝试指出您正确的方向。我认为如果您向我们展示您的数据库架构会更好。我很确定那里有一些改进空间。

1:你永远不应该真正使用驱动程序的名称,你应该有一个包含驱动程序名称的表,并且你有一个整数标识符字段,你将在其他表中用作外键。这大大加快了查询过程,使生活更轻松。特别是在某人意识到驱动程序名称中存在拼写错误且需要更新它的情况下?

2:看看JOIN,他们在做你正在做的事情上真的很有帮助。通过连接多个表,您可以轻松地按任何列进行排序。

3:使用fieldname IN('a' , 'b' , 'c')

而不是使用这么多OR

4:你应该真的得到一个好的IDE,它会让你和每个与你的代码相关的人生活更轻松,现在包括我们:)

答案 1 :(得分:0)

在我看来,你正在做一堆不同的查询,这些查询来自你的第一个查询(你获得了玩家的名字?)

您应该阅读加入表格。您可以在统一查询中更轻松地对所需数据进行排序。 除了 - 它不可排序,因为它基于你的第一个查询。

这是一个很好的入门书 http://www.tizag.com/mysqlTutorial/mysqljoins.php