English 中文(简体)
• 如何将10个询问优化成一个
原标题:How to optimize 10 queries into one
  • 时间:2012-04-18 09:42:40
  •  标签:
  • php
  • mysql

请问你如何优化我的询问。 我想从同一表格中抽取10个不同的数据。

www.un.org/spanish/ecosoc 这里是查询之一。

     $query1 = mysql_query("SELECT m.Course_code AS  Course , m.score, m.grade
                   FROM maintable AS m  
                   INNER JOIN students AS s ON
                   m.matric_no = s.matric_no
                   INNER JOIN Courses AS c ON
                   m.Course_code = c.Course_code
                   WHERE m.matric_no =  ".$matric_no." 
                   AND m.semester_name =  hamattarn 
                   AND m.level =  100  ") or die (mysql_error());

                  $number_cols1 = mysql_num_fields($query1) ;

<>查询的这一部分正在对所有询问进行改动。

  AND m.semester_name =  hamattarn 
                   AND m.level =  100  "

 AND m.semester_name =  rain 
                   AND m.level =  100  "

 AND m.semester_name =  hamattarn 
                   AND m.level =  200  "

 AND m.semester_name =  rain 
                   AND m.level =  200  "

  AND m.semester_name =  hamattarn 
                   AND m.level =  300  "

  AND m.semester_name =  rain 
                   AND m.level =  300  "

  AND m.semester_name =  hamattarn 
                   AND m.level =  400  "

  AND m.semester_name =  rain 
                   AND m.level =  400  "

 AND m.semester_name =  hamattarn 
                   AND m.level =  500  "

 AND m.semester_name =  rain 
                   AND m.level =  500  "

这里描述了我能以问答方式实现的目标。 在优化之后,我仍然希望它研究这种方式。

< href=“http://i.imgur.com/IGEj2.png”>Sample 地图

感谢你们的时间和耐心。

<>UPDATE——这里指的是,在优化之前,如何从每10个询问中显示浏览表。 请如何在新优化的提问中实现这种表格类型?

   echo "<table class="altrowstable" id = " bgcolor = gold >
";
     echo "<tr align=center>
";
     for ($i=0; $i<$number_cols10; $i++)
     {
               echo "<th>" . mysql_field_name($query10, $i). "</th>";
     }
     echo "</tr>
";
               while ($row = mysql_fetch_row($query10))
    {
    echo "<tr align=center>
";
    for ($i=0; $i<$number_cols10; $i++)
    {
    echo "<td>";
    if (!isset($row[$i]))
    {echo "NULL";}
    else
    {
    echo "<b>".$row[$i]."</b>";
    }
    echo "</td>
";
    }
    echo "</tr>
";
    }
    echo "</table>"; 
    echo"</span>" ;

感谢

最佳回答

Why not something like this:

WHERE m.semester_name IN ( rain ,  hamattarn ) 
  AND m.level IN ( 100 ,  200 ,  300 ,  400 ,  500 ))

由于结果好坏参半,你应按预期顺序取得结果,例如:

ORDER BY m.semester_name, m.level

这将使你能够把公共卫生和社会福利部的成果分成几个部分。

$previous_semester = $previous_level = false;
foreach(mysql_fetch_array($query) as $row) {
    if ($previous_semester == $row[ semester_name ]) {
        // row semester is different from the previous one
        echo $row[ semester_name ];
    }

    if ($previous_level == $row[ level ]) {
        // row level is different from previous one
        echo $row[ level ]; 
    }

    print_r($row);

    $previous_level = $row[ level ];
    $previous_semester = $row[ semester_name ];
}

You could open an HTML table before the for loop. When the semester or level changes, you could close the previous table and open a new one, add a title for the rest of the rows of the same semester/level, etc, etc... You could close an HTML table after the for loop.

它只是一小.,但最终会带你。

问题回答

如果你真想优化,那么就建立一个“karnaugh地图——一个单轴表,列出每个可能值的半年期——名称和具有一切可能价值的其他入口,则标明你希望列入你的询问的交叉点。 从这一点来看,你可以找出最简单的问题,以掩盖这一数据。

然而,由于你没有表明你想要排除哪些价值观,而且你已经把下半年——名称=irain,hamattarn}和水平=-1100,200,300,400,500}的所有可能组合都包括在内,那么简单的解决办法是:

WHERE semester_name IN ( rain ,  hamattarn )
AND level in (100,200,300,400,500)

如果你在条款的哪里需要列入上半年——姓名和级别,如果你的话,生命就会更加简单。

ORDER BY semester_name, level, course

仅通过您的WHERE条款获得所有可能的组合:

WHERE m.matric_no = :matric_no
AND (
       m.semester_name =  hamattarn 
    OR m.semester_name =  rain 
)
AND (
       m.level =  100 
    OR m.level =  200 
    OR m.level =  300 
    OR m.level =  400 
)




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签