Good all the time of day.

$result = mysql_query("SELECT COUNT(*), sum(q), sum(w), sum(e), sum(r) FROM zakaz WHERE d<'$d' AND c>'$c' AND a='$a' AND z='01'",$db); 

I have "many" such requests, with a difference only in z = 01, 02, 03, .... 99. Is it possible to somehow reduce the number of requests?

PS Ie do not write this into a cycle, which is shorter, but reduce the number of requests.

    2 answers 2

    The easiest:

     $result = mysql_query("SELECT COUNT(*), sum(q), sum(w), sum(e), sum(r) FROM zakaz WHERE d<'$d' AND c>'$c' AND a='$a' AND z IN ('01','02','03')",$db); //Π² скобках ΡƒΠ·ΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ всС Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΡ‹Π΅ значСния для z 

    Added by:

      //ΠΈΠΌΠ΅Π΅ΠΌ массив $z = array('01','02','03'); //ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΡƒΠ΅ΠΌ массивчик ΠΊΡƒΠ΄Π° Π±ΡƒΠ΄Π΅ΠΌ ΡΠΊΠ»Π°Π΄ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ Π²Ρ‹Π²ΠΎΠ΄. $out = array(); //Ρ„ΠΎΡ€ΠΌΠΈΡ€ΡƒΠ΅ΠΌ строку запроса: $query = "SELECT COUNT(*), sum(q) as sum_q, sum(w) as sum_w, sum(e) as sum_e, sum(r) as sum_r FROM zakaz WHERE d<'$d' AND c>'$c' AND a='$a' AND z IN ('".implode("','",$z)."') GROUP BY z"; //обращаСмся ΠΊ Π±Π°Π·Π΅ $result = mysql_query($query,$db); while ($row=mysql_fetch_assoc($result)) { //Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚: echo $row['sum_q']; //записываСм Π² Π²Ρ‹Ρ…ΠΎΠ΄Π½ΠΎΠΉ массив Ρ†Π΅Π»ΠΈΠΊΠΎΠΌ строку $out[] = $row; //Π»ΠΈΠ±ΠΎ ΠΊΠ°ΠΊΠΎΠ΅ Ρ‚ΠΎ ΠΎΠ΄Π½ΠΎ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ $out[] = $row['sum_q']; } 
    • After the request, I want to get the results of the request. Since I do not know how to make a request, and asked about it, it was possible to guess that getting the result of the query more complicated would also be problematic for me (logical conclusions of the CEP). How to get the value of the request after this? - sergey
    • Strange you have logical conclusions. The result of the query is obtained in the same way, and there is no direct connection with the complexity of the query. - DL_ pm
    • I learned a lot of new things, more understandable, but not completely. This is what I wrote with the top 495 variables (95 sums of quantity and 380 sums of values), well, by WISH, I can "echo" them. in the case of while, I can just see the result. How can the result be equated to variables? (the conclusion is NOT interested) - sergey
    • corrected - DL_
    • wrote $ out [03] = $ row ['sum_q']; and after the echo $ out [03] loop; ... for some values ​​(say 03, 04 ...) gives the correct amount, for which the amount is not correct. thanks anyway, I will continue to think for myself - sergey
     $result = mysql_query("SELECT COUNT(*), sum(q), sum(w), sum(e), sum(r), z FROM zakaz WHERE d<'$d' AND c>'$c' AND a='$a' GROUP BY z",$db); 

    Now the query will not be executed in a loop, this loop will be used when reading the results. If you need to somehow limit the field z, then use the condition, say "z BETWEEN '01' AND '99'"

    • after my installation, I had: $ myrow = mysql_fetch_array ($ result); $ sumq01 = $ myrow ['sum (q)']; this variable if z = 01. and now how? - sergey
    • print_r($myrow); and see everything - Sh4dow
    • Array ([0] => 0 [COUNT (*)] => 0 [1] => [sum (q)] => [2] => [sum (w)] => [3] => [sum (e)] => [4] => [sum (r)] =>) prescribed, it still did not reach. - sergey