简体   繁体   English

MySQL的Unknown列在where子句中?

[英]MySQL Unknown column in where clause?

I have two databases. 我有两个数据库。 One is called INFO with three tables ( Stories , Comments , Replies ) 其中一个称为INFO具有三个表( StoriesCommentsReplies

Stories has the following fields Stories具有以下字段

+--------------+----------------+------+-----+---------------------+-----------------------------+
| Field        | Type           | Null | Key | Default             | Extra                       |
+--------------+----------------+------+-----+---------------------+-----------------------------+
| storyID      | int(11)        | NO   | PRI | NULL                |                             |
| originalURL  | varchar(500)   | YES  |     | NULL                |                             |
| originalDate | timestamp      | NO   |     | CURRENT_TIMESTAMP   | on update CURRENT_TIMESTAMP |
| numDiggs     | int(11)        | YES  |     | NULL                |                             |
| numComments  | int(11)        | YES  |     | NULL                |                             |
| diggURL      | varchar(500)   | YES  |     | NULL                |                             |
| rating       | varchar(50)    | YES  |     | NULL                |                             |
| title        | varchar(200)   | YES  |     | NULL                |                             |
| summary      | varchar(10000) | YES  |     | NULL                |                             |
| uploaderID   | varchar(50)    | YES  |     | NULL                |                             |
| imageURL     | varchar(500)   | YES  |     | NULL                |                             |
| category1    | varchar(50)    | YES  |     | NULL                |                             |
| category2    | varchar(50)    | YES  |     | NULL                |                             |
| uploadDate   | timestamp      | NO   |     | 0000-00-00 00:00:00 |                             |
| num          | int(11)        | YES  |     | NULL                |                             |
+--------------+----------------+------+-----+---------------------+-----------------------------+

Another database is called Data with one table ( User ). 另一个数据库称为带有一个表的DataUser )。 Fields shown below: 如下所示的字段:

+-------------------+-------------+------+-----+---------+-------+
| Field             | Type        | Null | Key | Default | Extra |
+-------------------+-------------+------+-----+---------+-------+
| userID            | varchar(50) | NO   | PRI | NULL    |       |
| numStories        | int(11)     | YES  |     | NULL    |       |
| numComments       | int(11)     | YES  |     | NULL    |       |
| numReplies        | int(11)     | YES  |     | NULL    |       |
| numStoryDiggs     | int(11)     | YES  |     | NULL    |       |
| numCommentReplies | int(11)     | YES  |     | NULL    |       |
| numReplyDiggs     | int(11)     | YES  |     | NULL    |       |
| numStoryComments  | int(11)     | YES  |     | NULL    |       |
| numStoryReplies   | int(11)     | YES  |     | NULL    |       |
+-------------------+-------------+------+-----+---------+-------+

User.userID is full of thousands of unique names. User.userID充满了成千上万的唯一名称。 All other fields are currently NULL . 当前所有其他字段为NULL The names in User.userID correspond to the names in Stories.uploaderID . 在名称User.userID对应于名称Stories.uploaderID

I need to, for each userID in User , count the number of stories uploaded from (ie num) Stories for the corresponding name and insert this value into User.numStories . 我需要为每个userIDUser ,从数(即NUM)上传层数Stories为相应的名称,并插入该数值到User.numStories

The query which I have come up with (which produces an error) is: 我提出的查询(产生错误)是:

INSERT INTO DATA.User(numStories) 
SELECT count(num) 
FROM INFO.Stories 
WHERE INFO.Stories.uploaderID=DATA.User.userID;

The error I get when running this query is 运行此查询时出现的错误是

 Unknown column 'DATA.User.userID' in 'where clause'

Sorry if this is badly explained. 不好意思,如果不好解释。 I will try and re-explain if need be. 如果需要,我会尝试重新解释。

You aren't creating new entries in the User table, you're updating existing ones. 您不是在User表中创建新条目,而是在更新现有条目。 Hence, insert isn't the right syntax here, but rather update : 因此, insert不是正确的语法,而是update

UPDATE DATA.User u
JOIN   (SELECT   uploaderID, SUM(num) AS sumNum
        FROM     INFO.Stories
        GROUP BY uploadedID) i ON i.uploaderID = u.userID
SET    numStories = sumNum

EDIT: 编辑:
Some clarification, as requested in the comments. 根据评论中的要求进行了一些澄清。 The inner query sums the num in Stories per uploaderId . 内部查询求和numStoriesuploaderId The updates statement updates the numStories in User the the calculated sum of the inner query of the matching id . updates语句更新UsernumStories 匹配ID的内部查询的计算得出的总和。

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

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