SQL Combine Select, Update and Join into one query
I'm a beginner in sql and I am doing this through a php document. I need to know how to combine these into one query. The sql is rough but hopefully you all should get the idea of what I'm trying to do.
**What I'm trying to do is join the 'username' in the 'stats' and 'ufc133' tables then update the 'scores' table based on the value stored in 'pick1' from the 'ufc133' table
This is code for a sports picks website and my goal with this query is to give users scores based on the right or wrong picks they make. The scores and picks are stored in different tables. This particular query is just for 'pick1'
The Code:
<?php
mysql_query("
SELECT * FROM ufc133, stats JOIN stats ON ufc133.usern开发者_如何学Came = stats.username
UPDATE stats SET score = (score + 10), mmascore = (mmascore + 10), mmawins = (mmawins + 1), mmagames = (mmagames + 1), wins = (wins + 1), games = (games + 1)
WHERE pick1 = 11
");
?>
Updates statements can only reference one table. Your update can contain a subselect in the WHERE clause to match the ID of the rows you want to update with the result of some query. For example:
UPDATE stats
SET score = (score + 10), mmascore = (mmascore + 10), mmawins = (mmawins + 1), mmagames = (mmagames + 1), wins = (wins + 1), games = (games + 1)
-- This part made up for illustration purposes
WHERE id IN (
SELECT Table1.id from Table1 INNER JOIN Table2 ON Table1.id = Table2.id WHERE....
)
You cannot send separate/multiple statements via mysql_query()
.
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database
You're going to have to issue multiple commands, or call a stored procedure.
精彩评论