我在使SQL查询正常工作时遇到了一些麻烦。

首先要有一些背景知识...我正在处理具有一定数量可用点的事件,供用户注册活动。当用户注册一项活动时,该特定活动的位置数减少了一个。如果将用户从事件中删除,则无法将该活动的地点增加一个。

所以我的表如下。

Activities: this table is used to register users to a specific activity which is defined by table Activity

+----+------------+----------------+----------+
| ID | activityID | activityUserID | eventID  |
+----+------------+----------------+----------+
|  1 |      1     |          2     |       1  |
|  2 |      2     |          2     |       1  |
|  3 |      3     |          2     |       1  |
+----+------------+----------------+---------+


-ID是标识每一行的唯一键。

-activityID用于引用用户正在注册的活动(表Activity.ID)。例如:用户在事件1中注册活动3,活动ID为3,事件ID为1

-activityUserID是注册此事件的用户的ID

Activity: this table contains the activities and descriptions.

+----+---------------------+-------------+---------+
| ID | activityDescription | activityNum | eventID |
+----+---------------------+-------------+---------+
|  1 | test1               |      24     |       1 |
|  2 | test2               |      24     |       1 |
|  3 | test3               |      24     |       1 |
+----+---------------------+-------------+---------+


-ID是用于标识的唯一值。该值在表activity(activityID)中用于引用用户注册的活动

-activityNum是可用的开放点数

-eventID特定事件的唯一ID

这是我遇到麻烦的查询

 UPDATE activity
 SET activity.activityNum = activity.activityNum + 1
 WHERE activity.eventID = ".$eventID."  //$eventID is a obtained based on the page being viewed
 LEFT JOIN activities ON activity.eventID = activities.eventID
 WHERE activity.ID = activities.activityID

最佳答案

我同意Michael,但是您的Join语法不正确。正确执行您要求的方法是:

 UPDATE activity
 LEFT JOIN activities ON activity.eventID = activities.eventID
 SET activity.activityNum = activity.activityNum + 1
 WHERE activity.ID = activities.activityID AND activity.eventID = $eventID


尽管此语法会产生您尝试的结果,但我不确定它是否符合您的意图。我对表中正在执行的eventID感到困惑。是否应该将活动“分组”在一起?

关于mysql - 使用左联接的SQL查询遇到麻烦,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9406489/

10-12 17:11