Need help with an insert query.

  Post new topicReply to topicPrintable Version
<< View previous topic View next topic >>
Share: Del.icio.us  Digg  Google  Spurl  Blink  Furl  Y! MyWeb  
#1   Need help with an insert query.
Donovan
CZ Active Member
 Codezwiz Site Donator
Donovan has been a member for over 14 year's 14 Year Member
usa.gif ohio.gif
Occupation: Web Developer
Fav. Sports Team: St Louis Cardinals
Status: Offline
Joined: Dec 03, 2003
0.03 posts per day
Posts: 160
Points: 9,216
   
How can this query hard coded in phpMyAdmin to test:

SELECT Course_ID, SOMS_KEY, UID, Academic_Year, AVG( TL_Avg ) AS Final_Grade
FROM atlas_tl_session_grades sg
JOIN atlas_tl_session s ON ( s.Session_ID = sg.Session_ID )
WHERE s.Course_ID = 'CL00000011'
GROUP BY sg.SOMS_KEY



gives me 104 records which is what I want.

Yet this next query inserts 312 records?

$insertgrades = $db->sql_query("INSERT INTO ".$prefix."_tl_course_grades (Course_ID, SOMS_KEY, UID, Academic_Year, Final_Grade)". "SELECT Course_ID, SOMS_KEY, UID, Academic_Year, AVG(TL_Avg) AS FinalGrade
FROM ".$prefix."_tl_session_grades sg
JOIN ".$prefix."_tl_session s ON (s.Session_ID = sg.Session_ID)
WHERE s.Course_ID = '$Course_ID'
GROUP BY sg.SOMS_KEY");
if (!$insertgrades) {
      echo("<p>Error performing query: " . mysql_error() . "</p>");
      exit();   
      }   


I have 104 students with grades. 3 grades per student in session_grades. I need to average the TL_Avg field for all 3 records and insert this record into course_grades.


Back to top Reply with quote
#2   
Telli
Site Admin
Telli has been a member for over 15 year's 15 Year Member
Occupation: Self Employed
Age: 40
Fav. Sports Team: Detroit Red Wings
Website: www.codezhost.com
Status: Offline
Joined: May 26, 2003
1.48 posts per day
Posts: 8090
Points: 494,325
   
Did you enter in the second query correctly?


$insertgrades = $db->sql_query("INSERT INTO ".$prefix."_tl_course_grades (Course_ID, SOMS_KEY, UID, Academic_Year, Final_Grade)". "SELECT Course_ID, SOMS_KEY, UID, Academic_Year, AVG(TL_Avg) AS FinalGrade
FROM ".$prefix."_tl_session_grades sg
JOIN ".$prefix."_tl_session s ON (s.Session_ID = sg.Session_ID)
WHERE s.Course_ID = '$Course_ID'
GROUP BY sg.SOMS_KEY");
if (!$insertgrades) {
      echo("<p>Error performing query: " . mysql_error() . "</p>");
      exit();   
      }   


You won't be able to combine those 2 queries and there is a syntax error.

Do you have the insert query inside of the loop? Or is seperate from the select query?



_________________
The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he, who in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon thee. Ezekiel 25:17
Back to top Reply with quote
#3   re: Need help with an insert query.
Donovan
CZ Active Member
 Codezwiz Site Donator
Donovan has been a member for over 14 year's 14 Year Member
usa.gif ohio.gif
Occupation: Web Developer
Fav. Sports Team: St Louis Cardinals
Status: Offline
Joined: Dec 03, 2003
0.03 posts per day
Posts: 160
Points: 9,216
   
The second query "SELECT..." is correct as it gets all 104 records that I want. If I can't combine these two then can I separate them into something like:

$getgrades = $db->sql_query("SELECT Course_ID, SOMS_KEY, UID, Academic_Year, AVG(TL_Avg) AS FinalGrade
FROM ".$prefix."_tl_session_grades sg
JOIN ".$prefix."_tl_session s ON (s.Session_ID = sg.Session_ID)
WHERE s.Course_ID = '$Course_ID'
GROUP BY sg.SOMS_KEY");
if (!$getgrades) {
      echo("<p>Error performing query: " . mysql_error() . "</p>");
      exit();   
      }   
while($row = $db->sql_fetchrow($getgrades)) {
$Course_ID = $row[Course_ID];
$SOMS_KEY = $row[SOMS_KEY];
$UID = $row[UID];
$Academic_Year = $row[Academic_Year];
$FinalGrade = $row[FinalGrade];

$insertgrades = $db->sql_query("INSERT INTO ".$prefix."_tl_course_grades (Course_ID, SOMS_KEY, UID, Academic_Year, Final_Grade)". "VALUES ('$Course_ID', '$SOMS_KEY', '$UID', '$Academic_Year', '$FinalGrade')");
if (!$insertgrades) {
      echo("<p>Error performing query: " . mysql_error() . "</p>");
      exit();   
      }
      
}         


Unfortunately this give me the same 312 records as the last attempt did.


Back to top Reply with quote
#4   
Telli
Site Admin
Telli has been a member for over 15 year's 15 Year Member
Occupation: Self Employed
Age: 40
Fav. Sports Team: Detroit Red Wings
Website: www.codezhost.com
Status: Offline
Joined: May 26, 2003
1.48 posts per day
Posts: 8090
Points: 494,325
   
Maybe you misunderstood me, the second code box in your first post has this query:



$insertgrades = $db->sql_query("INSERT INTO ".$prefix."_tl_course_grades (Course_ID, SOMS_KEY, UID, Academic_Year, Final_Grade)". "SELECT Course_ID, SOMS_KEY, UID, Academic_Year, AVG(TL_Avg) AS FinalGrade
FROM ".$prefix."_tl_session_grades sg
JOIN ".$prefix."_tl_session s ON (s.Session_ID = sg.Session_ID)
WHERE s.Course_ID = '$Course_ID'
GROUP BY sg.SOMS_KEY");
if (!$insertgrades) {
      echo("<p>Error performing query: " . mysql_error() . "</p>");
      exit();   
      }   


Please look at that again and verify that it is correct.

Also try this and paste the the output in your response:


$getgrades = $db->sql_query("SELECT Course_ID, SOMS_KEY, UID, Academic_Year, AVG(TL_Avg) AS FinalGrade
FROM ".$prefix."_tl_session_grades sg
JOIN ".$prefix."_tl_session s ON (s.Session_ID = sg.Session_ID)
WHERE s.Course_ID = '$Course_ID'
GROUP BY sg.SOMS_KEY");
if (!$getgrades) {
      echo("<p>Error performing query: " . mysql_error() . "</p>");
      exit();   
}   
while($row = $db->sql_fetchrow($getgrades)) {
   $grades[] = $row;
}
print_r($grades);




_________________
The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he, who in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon thee. Ezekiel 25:17
Back to top Reply with quote
#5   
Donovan
CZ Active Member
 Codezwiz Site Donator
Donovan has been a member for over 14 year's 14 Year Member
usa.gif ohio.gif
Occupation: Web Developer
Fav. Sports Team: St Louis Cardinals
Status: Offline
Joined: Dec 03, 2003
0.03 posts per day
Posts: 160
Points: 9,216
   


Originally posted by Telli @ Mon Mar 24, 2008 5:18 pm:



Also try this and paste the the output in your response:


$getgrades = $db->sql_query("SELECT Course_ID, SOMS_KEY, UID, Academic_Year, AVG(TL_Avg) AS FinalGrade
FROM ".$prefix."_tl_session_grades sg
JOIN ".$prefix."_tl_session s ON (s.Session_ID = sg.Session_ID)
WHERE s.Course_ID = '$Course_ID'
GROUP BY sg.SOMS_KEY");
if (!$getgrades) {
      echo("<p>Error performing query: " . mysql_error() . "</p>");
      exit();   
}   
while($row = $db->sql_fetchrow($getgrades)) {
   $grades[] = $row;
}
print_r($grades);



It looks ugly.



Array ( [0] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 202 [SOMS_KEY] => 202 [2] => U00376103 [UID] => U00376103 [3] => 0709 [Academic_Year] => 0709 [4] => 78.333333 [FinalGrade] => 78.333333 ) [1] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 203 [SOMS_KEY] => 203 [2] => U00376118 [UID] => U00376118 [3] => 0709 [Academic_Year] => 0709 [4] => 92.333333 [FinalGrade] => 92.333333 ) [2] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 204 [SOMS_KEY] => 204 [2] => U00085285 [UID] => U00085285 [3] => 0709 [Academic_Year] => 0709 [4] => 91.666667 [FinalGrade] => 91.666667 ) [3] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 205 [SOMS_KEY] => 205 [2] => U00401324 [UID] => U00401324 [3] => 0709 [Academic_Year] => 0709 [4] => 91.000000 [FinalGrade] => 91.000000 ) [4] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 206 [SOMS_KEY] => 206 [2] => U00133110 [UID] => U00133110 [3] => 0709 [Academic_Year] => 0709 [4] => 84.333333 [FinalGrade] => 84.333333 ) [5] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 207 [SOMS_KEY] => 207 [2] => U00376134 [UID] => U00376134 [3] => 0709 [Academic_Year] => 0709 [4] => 86.333333 [FinalGrade] => 86.333333 ) [6] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 208 [SOMS_KEY] => 208 [2] => U00449534 [UID] => U00449534 [3] => 0709 [Academic_Year] => 0709 [4] => 88.666667 [FinalGrade] => 88.666667 ) [7] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 209 [SOMS_KEY] => 209 [2] => U00009736 [UID] => U00009736 [3] => 0709 [Academic_Year] => 0709 [4] => 84.333333 [FinalGrade] => 84.333333 ) [8] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 210 [SOMS_KEY] => 210 [2] => U00389364 [UID] => U00389364 [3] => 0709 [Academic_Year] => 0709 [4] => 81.000000 [FinalGrade] => 81.000000 ) [9] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 211 [SOMS_KEY] => 211 [2] => U00384541 [UID] => U00384541 [3] => 0709 [Academic_Year] => 0709 [4] => 90.333333 [FinalGrade] => 90.333333 ) [10] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 212 [SOMS_KEY] => 212 [2] => U00389029 [UID] => U00389029 [3] => 0709 [Academic_Year] => 0709 [4] => 76.666667 [FinalGrade] => 76.666667 ) [11] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 213 [SOMS_KEY] => 213 [2] => U00376195 [UID] => U00376195 [3] => 0709 [Academic_Year] => 0709 [4] => 93.666667 [FinalGrade] => 93.666667 ) [12] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 214 [SOMS_KEY] => 214 [2] => U00380589 [UID] => U00380589 [3] => 0709 [Academic_Year] => 0709 [4] => 85.333333 [FinalGrade] => 85.333333 ) [13] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 215 [SOMS_KEY] => 215 [2] => U00449400 [UID] => U00449400 [3] => 0709 [Academic_Year] => 0709 [4] => 81.666667 [FinalGrade] => 81.666667 ) [14] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 216 [SOMS_KEY] => 216 [2] => U00376197 [UID] => U00376197 [3] => 0709 [Academic_Year] => 0709 [4] => 91.000000 [FinalGrade] => 91.000000 ) [15] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 217 [SOMS_KEY] => 217 [2] => U00008189 [UID] => U00008189 [3] => 0709 [Academic_Year] => 0709 [4] => 91.666667 [FinalGrade] => 91.666667 ) [16] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 218 [SOMS_KEY] => 218 [2] => U00376198 [UID] => U00376198 [3] => 0709 [Academic_Year] => 0709 [4] => 87.333333 [FinalGrade] => 87.333333 ) [17] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 219 [SOMS_KEY] => 219 [2] => U00376200 [UID] => U00376200 [3] => 0709 [Academic_Year] => 0709 [4] => 81.666667 [FinalGrade] => 81.666667 ) [18] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 220 [SOMS_KEY] => 220 [2] => U00115187 [UID] => U00115187 [3] => 0709 [Academic_Year] => 0709 [4] => 75.333333 [FinalGrade] => 75.333333 ) [19] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 221 [SOMS_KEY] => 221 [2] => U00342132 [UID] => U00342132 [3] => 0709 [Academic_Year] => 0709 [4] => 83.333333 [FinalGrade] => 83.333333 ) [20] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 222 [SOMS_KEY] => 222 [2] => U00451266 [UID] => U00451266 [3] => 0709 [Academic_Year] => 0709 [4] => 88.333333 [FinalGrade] => 88.333333 ) [21] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 223 [SOMS_KEY] => 223 [2] => U00376201 [UID] => U00376201 [3] => 0709 [Academic_Year] => 0709 [4] => 88.333333 [FinalGrade] => 88.333333 ) [22] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 224 [SOMS_KEY] => 224 [2] => U00390322 [UID] => U00390322 [3] => 0709 [Academic_Year] => 0709 [4] => 83.333333 [FinalGrade] => 83.333333 ) [23] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 225 [SOMS_KEY] => 225 [2] => U00377202 [UID] => U00377202 [3] => 0709 [Academic_Year] => 0709 [4] => 82.333333 [FinalGrade] => 82.333333 ) [24] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 226 [SOMS_KEY] => 226 [2] => U00377205 [UID] => U00377205 [3] => 0709 [Academic_Year] => 0709 [4] => 90.333333 [FinalGrade] => 90.333333 ) [25] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 227 [SOMS_KEY] => 227 [2] => U00385207 [UID] => U00385207 [3] => 0709 [Academic_Year] => 0709 [4] => 89.333333 [FinalGrade] => 89.333333 ) [26] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 228 [SOMS_KEY] => 228 [2] => U00377212 [UID] => U00377212 [3] => 0709 [Academic_Year] => 0709 [4] => 83.000000 [FinalGrade] => 83.000000 ) [27] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 229 [SOMS_KEY] => 229 [2] => U00387689 [UID] => U00387689 [3] => 0709 [Academic_Year] => 0709 [4] => 87.333333 [FinalGrade] => 87.333333 ) [28] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 230 [SOMS_KEY] => 230 [2] => U00518290 [UID] => U00518290 [3] => 0709 [Academic_Year] => 0709 [4] => 92.333333 [FinalGrade] => 92.333333 ) [29] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 231 [SOMS_KEY] => 231 [2] => U00375805 [UID] => U00375805 [3] => 0709 [Academic_Year] => 0709 [4] => 86.000000 [FinalGrade] => 86.000000 ) [30] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 232 [SOMS_KEY] => 232 [2] => U00025880 [UID] => U00025880 [3] => 0709 [Academic_Year] => 0709 [4] => 85.333333 [FinalGrade] => 85.333333 ) [31] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 233 [SOMS_KEY] => 233 [2] => U00197801 [UID] => U00197801 [3] => 0709 [Academic_Year] => 0709 [4] => 91.000000 [FinalGrade] => 91.000000 ) [32] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 234 [SOMS_KEY] => 234 [2] => U00377216 [UID] => U00377216 [3] => 0709 [Academic_Year] => 0709 [4] => 95.000000 [FinalGrade] => 95.000000 ) [33] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 235 [SOMS_KEY] => 235 [2] => U00377221 [UID] => U00377221 [3] => 0709 [Academic_Year] => 0709 [4] => 90.000000 [FinalGrade] => 90.000000 ) [34] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 236 [SOMS_KEY] => 236 [2] => U00386413 [UID] => U00386413 [3] => 0709 [Academic_Year] => 0709 [4] => 85.000000 [FinalGrade] => 85.000000 ) [35] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 237 [SOMS_KEY] => 237 [2] => U00026420 [UID] => U00026420 [3] => 0709 [Academic_Year] => 0709 [4] => 88.666667 [FinalGrade] => 88.666667 ) [36] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 238 [SOMS_KEY] => 238 [2] => U00368248 [UID] => U00368248 [3] => 0709 [Academic_Year] => 0709 [4] => 88.000000 [FinalGrade] => 88.000000 ) [37] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 239 [SOMS_KEY] => 239 [2] => U00381655 [UID] => U00381655 [3] => 0709 [Academic_Year] => 0709 [4] => 83.333333 [FinalGrade] => 83.333333 ) [38] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 240 [SOMS_KEY] => 240 [2] => U00389568 [UID] => U00389568 [3] => 0709 [Academic_Year] => 0709 [4] => 88.333333 [FinalGrade] => 88.333333 ) [39] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 241 [SOMS_KEY] => 241 [2] => U00383348 [UID] => U00383348 [3] => 0709 [Academic_Year] => 0709 [4] => 87.333333 [FinalGrade] => 87.333333 ) [40] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 242 [SOMS_KEY] => 242 [2] => U00390387 [UID] => U00390387 [3] => 0709 [Academic_Year] => 0709 [4] => 88.666667 [FinalGrade] => 88.666667 ) [41] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 243 [SOMS_KEY] => 243 [2] => U00389574 [UID] => U00389574 [3] => 0709 [Academic_Year] => 0709 [4] => 82.333333 [FinalGrade] => 82.333333 ) [42] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 244 [SOMS_KEY] => 244 [2] => U00115663 [UID] => U00115663 [3] => 0709 [Academic_Year] => 0709 [4] => 88.333333 [FinalGrade] => 88.333333 ) [43] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 245 [SOMS_KEY] => 245 [2] => U00384327 [UID] => U00384327 [3] => 0709 [Academic_Year] => 0709 [4] => 90.666667 [FinalGrade] => 90.666667 ) [44] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 246 [SOMS_KEY] => 246 [2] => U00393044 [UID] => U00393044 [3] => 0709 [Academic_Year] => 0709 [4] => 91.333333 [FinalGrade] => 91.333333 ) [45] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 247 [SOMS_KEY] => 247 [2] => U00115836 [UID] => U00115836 [3] => 0709 [Academic_Year] => 0709 [4] => 83.333333 [FinalGrade] => 83.333333 ) [46] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 248 [SOMS_KEY] => 248 [2] => U00378120 [UID] => U00378120 [3] => 0709 [Academic_Year] => 0709 [4] => 93.000000 [FinalGrade] => 93.000000 ) [47] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 249 [SOMS_KEY] => 249 [2] => U00388880 [UID] => U00388880 [3] => 0709 [Academic_Year] => 0709 [4] => 89.333333 [FinalGrade] => 89.333333 ) [48] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 250 [SOMS_KEY] => 250 [2] => U00393112 [UID] => U00393112 [3] => 0709 [Academic_Year] => 0709 [4] => 85.000000 [FinalGrade] => 85.000000 ) [49] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 251 [SOMS_KEY] => 251 [2] => U00389363 [UID] => U00389363 [3] => 0709 [Academic_Year] => 0709 [4] => 84.333333 [FinalGrade] => 84.333333 ) [50] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 252 [SOMS_KEY] => 252 [2] => U00389266 [UID] => U00389266 [3] => 0709 [Academic_Year] => 0709 [4] => 83.666667 [FinalGrade] => 83.666667 ) [51] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 253 [SOMS_KEY] => 253 [2] => U00368308 [UID] => U00368308 [3] => 0709 [Academic_Year] => 0709 [4] => 88.333333 [FinalGrade] => 88.333333 ) [52] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 254 [SOMS_KEY] => 254 [2] => U00389967 [UID] => U00389967 [3] => 0709 [Academic_Year] => 0709 [4] => 88.666667 [FinalGrade] => 88.666667 ) [53] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 255 [SOMS_KEY] => 255 [2] => U00389968 [UID] => U00389968 [3] => 0709 [Academic_Year] => 0709 [4] => 92.666667 [FinalGrade] => 92.666667 ) [54] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 256 [SOMS_KEY] => 256 [2] => U00374985 [UID] => U00374985 [3] => 0709 [Academic_Year] => 0709 [4] => 84.333333 [FinalGrade] => 84.333333 ) [55] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 257 [SOMS_KEY] => 257 [2] => U00384570 [UID] => U00384570 [3] => 0709 [Academic_Year] => 0709 [4] => 83.666667 [FinalGrade] => 83.666667 ) [56] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 258 [SOMS_KEY] => 258 [2] => U00385332 [UID] => U00385332 [3] => 0709 [Academic_Year] => 0709 [4] => 83.666667 [FinalGrade] => 83.666667 ) [57] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 259 [SOMS_KEY] => 259 [2] => U00377223 [UID] => U00377223 [3] => 0709 [Academic_Year] => 0709 [4] => 85.666667 [FinalGrade] => 85.666667 ) [58] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 260 [SOMS_KEY] => 260 [2] => U00377226 [UID] => U00377226 [3] => 0709 [Academic_Year] => 0709 [4] => 85.333333 [FinalGrade] => 85.333333 ) [59] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 261 [SOMS_KEY] => 261 [2] => U00212284 [UID] => U00212284 [3] => 0709 [Academic_Year] => 0709 [4] => 84.333333 [FinalGrade] => 84.333333 ) [60] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 262 [SOMS_KEY] => 262 [2] => U00345826 [UID] => U00345826 [3] => 0709 [Academic_Year] => 0709 [4] => 86.333333 [FinalGrade] => 86.333333 ) [61] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 263 [SOMS_KEY] => 263 [2] => U00377227 [UID] => U00377227 [3] => 0709 [Academic_Year] => 0709 [4] => 87.666667 [FinalGrade] => 87.666667 ) [62] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 264 [SOMS_KEY] => 264 [2] => U00449530 [UID] => U00449530 [3] => 0709 [Academic_Year] => 0709 [4] => 80.333333 [FinalGrade] => 80.333333 ) [63] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 265 [SOMS_KEY] => 265 [2] => U00389027 [UID] => U00389027 [3] => 0709 [Academic_Year] => 0709 [4] => 91.000000 [FinalGrade] => 91.000000 ) [64] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 266 [SOMS_KEY] => 266 [2] => U00377198 [UID] => U00377198 [3] => 0709 [Academic_Year] => 0709 [4] => 92.333333 [FinalGrade] => 92.333333 ) [65] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 267 [SOMS_KEY] => 267 [2] => U00382460 [UID] => U00382460 [3] => 0709 [Academic_Year] => 0709 [4] => 79.333333 [FinalGrade] => 79.333333 ) [66] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 268 [SOMS_KEY] => 268 [2] => U00275488 [UID] => U00275488 [3] => 0709 [Academic_Year] => 0709 [4] => 86.666667 [FinalGrade] => 86.666667 ) [67] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 269 [SOMS_KEY] => 269 [2] => U00377211 [UID] => U00377211 [3] => 0709 [Academic_Year] => 0709 [4] => 86.666667 [FinalGrade] => 86.666667 ) [68] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 270 [SOMS_KEY] => 270 [2] => U00377213 [UID] => U00377213 [3] => 0709 [Academic_Year] => 0709 [4] => 82.000000 [FinalGrade] => 82.000000 ) [69] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 271 [SOMS_KEY] => 271 [2] => U00212286 [UID] => U00212286 [3] => 0709 [Academic_Year] => 0709 [4] => 93.333333 [FinalGrade] => 93.333333 ) [70] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 272 [SOMS_KEY] => 272 [2] => U00389021 [UID] => U00389021 [3] => 0709 [Academic_Year] => 0709 [4] => 89.333333 [FinalGrade] => 89.333333 ) [71] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 273 [SOMS_KEY] => 273 [2] => U00079690 [UID] => U00079690 [3] => 0709 [Academic_Year] => 0709 [4] => 84.333333 [FinalGrade] => 84.333333 ) [72] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 274 [SOMS_KEY] => 274 [2] => U00103050 [UID] => U00103050 [3] => 0709 [Academic_Year] => 0709 [4] => 83.333333 [FinalGrade] => 83.333333 ) [73] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 275 [SOMS_KEY] => 275 [2] => U00389354 [UID] => U00389354 [3] => 0709 [Academic_Year] => 0709 [4] => 82.333333 [FinalGrade] => 82.333333 ) [74] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 276 [SOMS_KEY] => 276 [2] => U00377190 [UID] => U00377190 [3] => 0709 [Academic_Year] => 0709 [4] => 87.333333 [FinalGrade] => 87.333333 ) [75] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 277 [SOMS_KEY] => 277 [2] => U00377191 [UID] => U00377191 [3] => 0709 [Academic_Year] => 0709 [4] => 81.666667 [FinalGrade] => 81.666667 ) [76] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 278 [SOMS_KEY] => 278 [2] => U00394718 [UID] => U00394718 [3] => 0709 [Academic_Year] => 0709 [4] => 79.333333 [FinalGrade] => 79.333333 ) [77] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 279 [SOMS_KEY] => 279 [2] => U00377170 [UID] => U00377170 [3] => 0709 [Academic_Year] => 0709 [4] => 84.333333 [FinalGrade] => 84.333333 ) [78] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 280 [SOMS_KEY] => 280 [2] => U00377172 [UID] => U00377172 [3] => 0709 [Academic_Year] => 0709 [4] => 86.666667 [FinalGrade] => 86.666667 ) [79] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 281 [SOMS_KEY] => 281 [2] => U00389025 [UID] => U00389025 [3] => 0709 [Academic_Year] => 0709 [4] => 89.333333 [FinalGrade] => 89.333333 ) [80] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 282 [SOMS_KEY] => 282 [2] => U00073621 [UID] => U00073621 [3] => 0709 [Academic_Year] => 0709 [4] => 85.333333 [FinalGrade] => 85.333333 ) [81] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 283 [SOMS_KEY] => 283 [2] => U00522536 [UID] => U00522536 [3] => 0709 [Academic_Year] => 0709 [4] => 85.333333 [FinalGrade] => 85.333333 ) [82] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 284 [SOMS_KEY] => 284 [2] => U00377181 [UID] => U00377181 [3] => 0709 [Academic_Year] => 0709 [4] => 88.333333 [FinalGrade] => 88.333333 ) [83] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 285 [SOMS_KEY] => 285 [2] => U00377184 [UID] => U00377184 [3] => 0709 [Academic_Year] => 0709 [4] => 91.333333 [FinalGrade] => 91.333333 ) [84] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 286 [SOMS_KEY] => 286 [2] => U00221744 [UID] => U00221744 [3] => 0709 [Academic_Year] => 0709 [4] => 92.666667 [FinalGrade] => 92.666667 ) [85] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 287 [SOMS_KEY] => 287 [2] => U00376853 [UID] => U00376853 [3] => 0709 [Academic_Year] => 0709 [4] => 88.333333 [FinalGrade] => 88.333333 ) [86] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 288 [SOMS_KEY] => 288 [2] => U00055638 [UID] => U00055638 [3] => 0709 [Academic_Year] => 0709 [4] => 81.000000 [FinalGrade] => 81.000000 ) [87] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 289 [SOMS_KEY] => 289 [2] => U00072716 [UID] => U00072716 [3] => 0709 [Academic_Year] => 0709 [4] => 92.666667 [FinalGrade] => 92.666667 ) [88] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 290 [SOMS_KEY] => 290 [2] => U00376182 [UID] => U00376182 [3] => 0709 [Academic_Year] => 0709 [4] => 86.666667 [FinalGrade] => 86.666667 ) [89] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 291 [SOMS_KEY] => 291 [2] => U00116761 [UID] => U00116761 [3] => 0709 [Academic_Year] => 0709 [4] => 77.333333 [FinalGrade] => 77.333333 ) [90] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 292 [SOMS_KEY] => 292 [2] => U00391968 [UID] => U00391968 [3] => 0709 [Academic_Year] => 0709 [4] => 86.000000 [FinalGrade] => 86.000000 ) [91] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 293 [SOMS_KEY] => 293 [2] => U00370520 [UID] => U00370520 [3] => 0709 [Academic_Year] => 0709 [4] => 88.666667 [FinalGrade] => 88.666667 ) [92] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 294 [SOMS_KEY] => 294 [2] => U00376147 [UID] => U00376147 [3] => 0709 [Academic_Year] => 0709 [4] => 90.333333 [FinalGrade] => 90.333333 ) [93] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 295 [SOMS_KEY] => 295 [2] => U00212293 [UID] => U00212293 [3] => 0709 [Academic_Year] => 0709 [4] => 80.333333 [FinalGrade] => 80.333333 ) [94] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 296 [SOMS_KEY] => 296 [2] => U00376158 [UID] => U00376158 [3] => 0709 [Academic_Year] => 0709 [4] => 88.333333 [FinalGrade] => 88.333333 ) [95] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 297 [SOMS_KEY] => 297 [2] => U00383573 [UID] => U00383573 [3] => 0709 [Academic_Year] => 0709 [4] => 75.333333 [FinalGrade] => 75.333333 ) [96] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 298 [SOMS_KEY] => 298 [2] => U00381902 [UID] => U00381902 [3] => 0709 [Academic_Year] => 0709 [4] => 86.333333 [FinalGrade] => 86.333333 ) [97] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 299 [SOMS_KEY] => 299 [2] => U00376160 [UID] => U00376160 [3] => 0709 [Academic_Year] => 0709 [4] => 84.000000 [FinalGrade] => 84.000000 ) [98] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 300 [SOMS_KEY] => 300 [2] => U00387802 [UID] => U00387802 [3] => 0709 [Academic_Year] => 0709 [4] => 89.666667 [FinalGrade] => 89.666667 ) [99] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 301 [SOMS_KEY] => 301 [2] => U00380574 [UID] => U00380574 [3] => 0709 [Academic_Year] => 0709 [4] => 90.666667 [FinalGrade] => 90.666667 ) [100] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 302 [SOMS_KEY] => 302 [2] => U00376161 [UID] => U00376161 [3] => 0709 [Academic_Year] => 0709 [4] => 90.666667 [FinalGrade] => 90.666667 ) [101] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 415 [SOMS_KEY] => 415 [2] => U00102136 [UID] => U00102136 [3] => 0709 [Academic_Year] => 0709 [4] => 84.333333 [FinalGrade] => 84.333333 ) [102] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 419 [SOMS_KEY] => 419 [2] => U00449535 [UID] => U00449535 [3] => 0709 [Academic_Year] => 0709 [4] => 88.666667 [FinalGrade] => 88.666667 ) [103] => Array ( [0] => CL00000011 [Course_ID] => CL00000011 [1] => 425 [SOMS_KEY] => 425 [2] => U00595993 [UID] => U00595993 [3] => 0709 [Academic_Year] => 0709 [4] => 74.500000 [FinalGrade] => 74.500000 ) )



Back to top Reply with quote
#6   re: Need help with an insert query.
Donovan
CZ Active Member
 Codezwiz Site Donator
Donovan has been a member for over 14 year's 14 Year Member
usa.gif ohio.gif
Occupation: Web Developer
Fav. Sports Team: St Louis Cardinals
Status: Offline
Joined: Dec 03, 2003
0.03 posts per day
Posts: 160
Points: 9,216
   
This ran in phpMyadmin

INSERT INTO atlas_tl_course_grades( Course_ID, Course_Number, SOMS_KEY, UID, Academic_Year, Final_Grade )
SELECT Course_ID, Course_Number, SOMS_KEY, UID, Academic_Year, AVG( TL_Avg ) AS FinalGrade
FROM atlas_tl_session_grades sg
JOIN atlas_tl_session s ON ( s.Session_ID = sg.Session_ID )
WHERE s.Course_Number = '543'
GROUP BY sg.SOMS_KEY


..results in 104 records inserted.

This


$insertgrades = "INSERT INTO atlas_tl_course_grades( Course_ID, Course_Number, SOMS_KEY, UID, Academic_Year, Final_Grade )
SELECT Course_ID, Course_Number, SOMS_KEY, UID, Academic_Year, AVG( TL_Avg ) AS FinalGrade
FROM atlas_tl_session_grades sg
JOIN atlas_tl_session s ON ( s.Session_ID = sg.Session_ID )
WHERE s.Course_Number = '$Course_Number'
GROUP BY sg.SOMS_KEY";
$sql = $db->sql_query($insertgrades);
if (!$sql) {
      echo("<p>Error performing query: " . mysql_error() . "</p>");
      exit();   
      }



results in 312 records inserted.

I have echoed the value for Course_Number and it is in fact 543. I don't understand why this statement ran in a PHP script would insert the same record 3 times resulting in 312, yet ran in SQL it inserts the correct 104 records once.


Back to top Reply with quote
#7   re: Need help with an insert query.
Donovan
CZ Active Member
 Codezwiz Site Donator
Donovan has been a member for over 14 year's 14 Year Member
usa.gif ohio.gif
Occupation: Web Developer
Fav. Sports Team: St Louis Cardinals
Status: Offline
Joined: Dec 03, 2003
0.03 posts per day
Posts: 160
Points: 9,216
   
After banging away at this all day I found what was happening.

I had an extra set of
{

}

that was looping.



Back to top Reply with quote
Display posts from previous:      
Add To: Del.icio.us  Digg  Google  Spurl  Blink  Furl  Y! MyWeb  
<< View previous topic View next topic >>
Post new topicReply to topic

Jump to 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum