1

There are many questions on SO about this but I cannot find one that quite meets my situation.

I want to use the values in some fields/columns of a table to set the value of a third field/column

In other words something like:

table races
athleteid|difficulty|score|adjustedscore

$sqlSelect = "SELECT athleteid,difficulty,score FROM races";
$res = mysql_query($sqlSelect) or die(mysql_error());
while ($row = mysql_fetch_array($res)){

$adjustedscore=difficulty*score;

$sqlupdate = "UPDATE race, set adjustedscore = '$adjustedscore' WHERE athletes = 'athletes'";
$resupdate = mysql_query($sqlupdate);

}

My understanding, however, is that MYSQL does not support update queries nested in select ones.

Note, I have simplified this slightly. I am actually calculating the score based on a lot of other variables as well--and may join some tables to get other inputs--but this is the basic principal.

Thanks for any suggestions

zztop
  • 701
  • 1
  • 7
  • 20
  • instead of update nested in select would you not have a select nested inside of the update query? – camdixon Feb 13 '17 at 21:33
  • that might be the way to do it. But it would have to be in a loop through ids as I need to do this for all of the records. – zztop Feb 13 '17 at 21:34
  • Why is this tagget as iOS ??? –  Feb 13 '17 at 21:34
  • 3
    And another kitten bites the dust – Strawberry Feb 13 '17 at 21:35
  • mistake. Was supposed to be php. Changed. – zztop Feb 13 '17 at 21:35
  • ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Feb 13 '17 at 21:36
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Feb 13 '17 at 21:37
  • You have syntax errors - curly quotes, missing quotes. – Jay Blanchard Feb 13 '17 at 21:37
  • Change this:`set adjustedscore = '$adjustedscore WHERE`... for this `set adjustedscore = '$adjustedscore' WHERE`... – Danilo Bustos Feb 13 '17 at 21:40
  • This is just a simple UPDATE. No SELECT, no loop required – Strawberry Feb 13 '17 at 21:40
  • Um @jayblanchard but there needn't be. It's redundant. – Strawberry Feb 13 '17 at 22:05
  • Ah - I see that now @Strawberry. Mind addled on Monday. – Jay Blanchard Feb 13 '17 at 22:07

2 Answers2

2

You can run:

UPDATE `races`
SET `adjustedscore` = `difficulty` * `score`
WHERE `athleteid` IN (1, 2, 3, ...)
Jirka Hrazdil
  • 3,983
  • 1
  • 14
  • 17
0

First of all, as previous commentators said, you should use PDO instead of mysql_* queries. Read about PDO here.
When you'll get data from DB with your SELECT query, you'll get array. I recommend you to use fetchAll() from PDO documentation.
So, your goal is to save this data in some variable. Like you did with $row.
After that you'll need to loop over each array and get your data:
foreach($row as $r) { //We do this to access each of ours athlete data $adjustedscore= $row[$r]["difficulty"]* $row[$r]["score"]; //Next row is not clear for me... $query = "UPDATE race SET adjustedscore = '$adjustedscore' WHERE athletes = 'athletes'";
And to update we use PDO update prepared statement
$stmt = $dbh->prepare($query); $stmt->execute(); }

Grynets
  • 2,477
  • 1
  • 17
  • 41
  • Selecting this answer over Jiri's which also worked as it will be easier to do calculations in php (as I add more logic in calculating adjusted score) than in MYSQL. But both approaches seem valid. – zztop Feb 13 '17 at 22:24