2017-05-23 4 views
1

mein Projekt ist eine Frage Papiergenerator für jedes Fragezeichen kann je nach Frage Typ variieren, ich muss einige zufällige Frage herausfiltern, basierend auf der MarkenfeldZeilen zurückgeben, wo die Summe einer einzelnen Spalte gleich "16" ist in mysql

zB

nur eine Tabelle

Questions | Mark 
q1  | 4 
q2  | 4 
q3  | 8 
q4  | 6 
q5  | 12 
q6  | 2 

ich möchte die Zeilen auszuwählen, wenn wir SUM (Marke) gleich 16

kann das Ergebnis var dh wie als

q1 | 4 
q2 | 4 
q4 | 6 
q5 | 2 

folgt, wenn wir dieses Ergebnis summieren wir 16 wie folgt Ich brauche eine Abfrage erhalten

meine Frage ist

Select question, mark 
from table 
where sum(mark) = 16 
+0

hinzufügen Tabellendetails (col Name, Zeilenname etc.) –

+0

mich nicht ganz verstehen, was Ihr erwartetes Ergebnis ist – Swellar

+1

warum nicht '4 + 4 + 8 = 16' OR' 4 + 12'? Was ist die Logik? –

Antwort

0

Der Aufwand wird durch den Computer durchgeführt werden, aber Wenn Sie eine große Menge an Daten haben, nehmen Sie in der Zwischenzeit einen Kaffee.

/* ===== TAKEN FROM PHP.net ====== */ 
$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); 

/* check connection */ 
if ($mysqli->connect_errno) { 
    printf("Connect failed: %s\n", $mysqli->connect_error); 
    exit(); 
} 

$query = "Select question, mark from table"; // your query 

$arrayData = array();//set an empty array to populate 

if ($result = $mysqli->query($query)) { 

    /* fetch associative array */ 
    while ($row = $result->fetch_assoc()) { 

     //for calculation purpose, we force the Mark field to be an integer 
     $arrayData[$row['question']] = (int)$row['mark']; //populate array with sql results 
    } 

    /* free result set */ 
    $result->free(); 
} 

/* close connection */ 
$mysqli->close(); 

Jetzt haben wir die SQL-Ergebnisse in der $arrayData Variable. So:

Array("q1"=>4, "q2"=>4, "q3"=>8) 

und so weiter ...

Gehen wir.

$targetSUM = 16; 
//First of all, remove any doubt and see 
//if the sum of the entire array is the same as our target 

if($targetSUM === array_sum($arrayData)){ 
    print_r($arrayData); 
    die(); 
} 
$i=0; 
$results = array(); 
foreach($arrayData as $questionA=>$markA){ 

    $thisCycle = array($questionA=>$markA); 
    foreach($arrayData as $questionB=>$markB){ 
     if ($questionA === $questionB) continue; // avoid sum SUM itself 

     $thisCycle[$questionB] = $markB; 

     $thisCycleSUM = array_sum($thisCycle); 

     switch(true){ 
      case $thisCycleSUM < $targetSUM: //if results not reach our target, go on 
       continue; 
      break; 
      case $thisCycleSUM === $targetSUM: //if results match, store and go on 
       $results[$i] = $thisCycle; 
       $thisCycle = array($questionA=>$markA); 
      break; 
      case $thisCycleSUM > $targetSUM: //if results exceeds, remove last element and go on 
       unset($thisCycle[$questionB]); 
      break; 
     } 

    } 


     // Sorting procedure to avoid same combinations and no-combination 
    if(isset($results[$i])){ 
     ksort($results[$i]); 
     $results[implode(array_keys($results[$i]))] = $results[$i]; 
     unset($results[$i]); 
     $i++; 
    } 

} 
    $results = array_values($results); 
    print_r($results); 

DEMO HERE

+0

Ihr Code spart mich. Danke Kumpel, das kann in Zukunft vielen helfen. – Aravindan

+0

Gern geschehen. Denken Sie daran, die Antwort als gültig zu markieren, um vielen in Zukunft zu helfen –

+1

Sie haben einen Fehler, wenn Sie 'q7 = 1' hinzufügen http://rextester.com/CJGZ38859 –

Verwandte Themen