Jump to content

SELECT (value / value) * variable) AS variable


Donovan

Recommended Posts

Somebody help me with this query.  I have a raw score from a test.  I get the number of questions for this test and then try to update the grade.

 

This gets the number of questions that were on the test.

 

$getnumquestions = $db->sql_query("SELECT num_quest_irat FROM ".$prefix."_tl_session WHERE Session_ID = '$Session_ID'");
while($info = $db->sql_fetchrow($getnumquestions)) {
$NumQuestions = $info['num_quest_irat'];
}  

 

The next code is trying to update the table and set a value for IRAT_Grade.

 

$updateIRAT_Grade = $db->sql_query("UPDATE ".$prefix."_tl_session_grades sg
JOIN ".$prefix."_tl_session s
SET IRAT_Grade = (SELECT ((IRAT_Raw / $NumQuestions) * 100) AS IRATGrade)
WHERE sg.Session_ID = s.Session_ID");
if (!$updateIRAT_Grade) {
	echo("<p>Error performing query: " . mysql_error() . "</p>");
	exit();   
	}

 

Currently I am getting an error:

 

Error performing query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') * 100) AS IRATGrade) WHERE sg.Session_ID = s.Session_ID' at line 3

Link to comment
https://forums.phpfreaks.com/topic/96711-select-value-value-variable-as-variable/
Share on other sites

Also I think I can have multple SET commands in one query.

 

True?

 

$updateGrade = $db->sql_query("UPDATE ".$prefix."_tl_session_grades sg
JOIN ".$prefix."_tl_session s
SET IRAT_Grade = (SELECT ((IRAT_Raw / $NumQuestions) * 100) AS IRATGrade)
SET GRAT_Grade = (SELECT ((GRAT_Raw / $NumQuestions) * 100) AS GRATGrade)
SET AppEx_Grade = (SELECT ((Appex_Raw / $NumQuestions) * 100) AS AppexGrade)
WHERE sg.Session_ID = s.Session_ID");
if (!$updateGrade) {
echo("<p>Error performing query: " . mysql_error() . "</p>");	
exit(); 

 

}

I'm not following you.  Your reply is to vague.

 

Do you mean something like this?

 

$updateGrade = $db->sql_query("UPDATE ".$prefix."_tl_session_grades
SET IRAT_Grade = (SELECT ((IRAT_Raw / $IratQuestions) * 100) AS IRATGrade),
SET GRAT_Grade = (SELECT ((GRAT_Raw / $GratQuestions) * 100) AS GRATGrade),
SET AppEx_Grade = (SELECT ((Appex_Raw / $AppExQuestions) * 100) AS AppexGrade)
WHERE Session_ID = '$Session_ID'");
if (!$updateGrade) {
echo("<p>Error performing query: " . mysql_error() . "</p>");	
exit();
}

No, I mean:

updateGrade = $db->sql_query("UPDATE ".$prefix."_tl_session_grades
SET IRAT_Grade = (SELECT ((IRAT_Raw / $IratQuestions) * 100) AS IRATGrade),
GRAT_Grade = (SELECT ((GRAT_Raw / $GratQuestions) * 100) AS GRATGrade),
AppEx_Grade = (SELECT ((Appex_Raw / $AppExQuestions) * 100) AS AppexGrade)
WHERE Session_ID = '$Session_ID'");
if (!$updateGrade) {
echo("<p>Error performing query: " . mysql_error() . "</p>");	
exit();
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.