简体   繁体   English

SQL将选择,更新和联接合并为一个查询

[英]SQL Combine Select, Update and Join into one query

I'm a beginner in sql and I am doing this through a php document. 我是sql的初学者,我正在通过php文档进行此操作。 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. sql很粗糙,但是希望大家都应该对我正在尝试做的事情有所了解。

**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 **我要执行的操作是在“ stats”和“ ufc133”表中加入“用户名”,然后根据“ ufc133”表中“ pick1”中存储的值更新“得分”表

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' 此特定查询仅适用于“ pick1”

The Code: 编码:

<?php

mysql_query("
SELECT * FROM ufc133, stats JOIN stats ON ufc133.username = 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. 您的更新可以在WHERE子句中包含一个子选择 ,以使您要更新的行的ID与某些查询的结果相匹配。 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()发送单独/多个语句。

mysql_query() sends a unique query (multiple queries are not supported) to the currently active database mysql_query()向当前活动的数据库发送一个唯一的查询(不支持多个查询)

You're going to have to issue multiple commands, or call a stored procedure. 您将必须发出多个命令或调用存储过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM