在我建,我還沒有想出一個辦法拿出一個SQL語句,以使此任務可以檢查應用程序之一進行計算。我國的評分制度只需要計算7個科目。在這些科目中,所有語言都是強制性的(圖示中標有lang)。計算出2門最好的科學(標記爲sci)。一個人類(標籤爲嗡嗡聲)。第七個主題可能是技術科目(標示爲科技),科學或人類取決於哪個科目具有最佳標記。例如,給定表中的學生將會記錄來自MAT,ENG,KISW,PHY,CHEM,HIST和BST的分數。我使用數組編寫了PHP代碼,它佔用了太多的內存和時間,這讓我擔心它會讓服務器停下來。看看下面的代碼,使用SQL基於某些給定的標準
function calculatePointsB($adm,$term,$class,$year,$exam){
global $db;
$points=array(
"A"=>12,
"A-"=>11,
"B+"=>10,
"B"=>9,
"B-"=>8,
"C+"=>7,
"C"=>6,
"C-"=>5,
"D+"=>4,
"D"=>3,
"D-"=>2,
"E"=>1
);
$choices=array();
$countable=array();
$monitor=array();
$grades=array();
$common_query_all=$db->prepare("SELECT marks,subject,sub_cat FROM averaged_marks WHERE adm_no=? AND sub_cat=? AND term=? AND class=? AND year=?");
$common_query_sp=$db->prepare("SELECT marks,subject,sub_cat FROM exmarks WHERE adm_no=? AND sub_cat=? AND term=? AND class=? AND year=? AND e_type=?");
//languages first
$langs=array();
if($exam=="All") {
$getlang = $common_query_all;
$getlang->execute(array($adm, "lang", $term, $class, $year));
}else{
$getlang = $common_query_sp;
$getlang->execute(array($adm, "lang", $term, $class, $year,$exam));
}
while($rst=$getlang->fetch(PDO::FETCH_ASSOC)){
if($rst['marks']!=null) {
$langs[$rst['subject']] = $rst['marks'];
$grades[$rst['subject']]=$points[$rst['grade']];
$countable[$rst['subject']] = $rst['marks'];
array_push($monitor,$rst['sub_cat']);
}else continue;
} unset($rst);
//sciences
$sciences=array();
if($exam=="All") {
$getlang = $common_query_all;
$getlang->execute(array($adm, "sci", $term, $class, $year));
}else{
$getlang = $common_query_sp;
$getlang->execute(array($adm, "sci", $term, $class, $year,$exam));
}
while($rst=$getlang->fetch(PDO::FETCH_ASSOC)){
if($rst['marks']!=null) {
$sciences[$rst['subject']] = $rst['marks'];
array_push($monitor,$rst['sub_cat']);
}else continue;
}unset($rst);
arsort($sciences);
//humanities
$humanities=array();
if($exam=="All") {
$getlang = $common_query_all;
$getlang->execute(array($adm, "hum", $term, $class, $year));
}else{
$getlang = $common_query_sp;
$getlang->execute(array($adm, "hum", $term, $class, $year,$exam));
}
while($rst=$getlang->fetch(PDO::FETCH_ASSOC)){
if($rst['marks']!=null) {
$humanities[$rst['subject']] = $rst['marks'];
array_push($monitor,$rst['sub_cat']);
}else continue;
}unset($rst);
arsort($humanities);
//technicals
$technic=array();
if($exam=="All") {
$getlang = $common_query_all;
$getlang->execute(array($adm, "tech", $term, $class, $year));
}else{
$getlang = $common_query_sp;
$getlang->execute(array($adm, "tech", $term, $class, $year,$exam));
}
while($rst=$getlang->fetch(PDO::FETCH_ASSOC)){
if($rst['marks']!=null) {
$technic[$rst['subject']] = $rst['marks'];
$choices[$rst['subject']] = $rst['marks'];
}else continue;
}unset($rst);
arsort($technic);
//add optional subjects to choices array
$iteration=0;
foreach($humanities as $sub => $value){
$iteration++;
if($iteration >1 && $iteration <= 3){
$choices[$sub]=$value;
}elseif($iteration==1){
$countable[$sub]=$value;
if($exam=="All") {
$grades[$sub] = $points[getgradeinSub($adm, $sub, $term, $class, $year)];
}else{
$grades[$sub] = $points[getgradeinSpSub($adm, $sub, $term, $class, $year,$exam)];
}
}
}unset($humanities);
$iteration=0;
foreach($sciences as $sub => $value){
$iteration++;
if($iteration > 2 && $iteration <= 3){
$choices[$sub]=$value;
}elseif($iteration<=2){
$countable[$sub]=$value;
if($exam=="All") {
$grades[$sub] = $points[getgradeinSub($adm, $sub, $term, $class, $year)];
}else{
$grades[$sub] = $points[getgradeinSpSub($adm, $sub, $term, $class, $year,$exam)];
}
}
}unset($sciences);
arsort($choices);
//get required and view the required number
//count number of keys
$count_compasory=array_count_values($monitor);
if($count_compasory['lang']==3 && $count_compasory['sci']>=2 && $count_compasory['hum']>=1){
$remaining=7-count($countable);
$iteration=0;
foreach($choices as $sub => $value){
$iteration++;
if($iteration <= $remaining){
$countable[$sub]=$value;
if($exam=="All") {
$grades[$sub] = $points[getgradeinSub($adm, $sub, $term, $class, $year)];
}else{
$grades[$sub] = $points[getgradeinSpSub($adm, $sub, $term, $class, $year,$exam)];
}
}
}
unset($choices);
unset($countable);
unset($monitor);
unset($langs);
unset($sciences);
unset($humanities);
unset($points);
return array_sum($grades);
}
else unset($choices);
unset($countable);
unset($monitor);
unset($langs);
unset($sciences);
unset($humanities);
unset($grades);
unset($points);
return null;}
一個好的程序員會認爲這是一些不好的做法。有沒有辦法爲此編寫SQL語句?解決這個問題將是一個突破。下面附有表格的圖片。
向我們提供一個sqlfiddle(http://sqlfiddle.com/)與(一個學生的)一些數據回答這個問題將極大地幫助。 對我來說這似乎應該可以通過一個選擇查詢來解決,同一張桌子上有幾個內部聯接 – Jester
什麼是您的DBMS?應該是一個簡單的任務,如果它支持像'ROW_NUMBER' – dnoeth
@Jester解析函數,這裏是SQL小提琴http://sqlfiddle.com/#!9/b80692 –