使用来自多个表的数据更新mysql表

时间:2013-11-17 17:20:47

标签: mysql sql

我有以下查询,我想这样做,如果有重复的密钥,它会更新值

INSERT INTO totalData (pageId, dateScanned, totalPageLikes, totalTalkingAbout, totalPos, totalNeg, totalFemales, totalMales, totalStrongPositives, totalPositives, totalWeakPositives, totalNeutrals, totalWeakNegatives, totalNegatives, totalStrongNegatives, totalStatuses, totalStatusLikes, totalStatusShares, totalComments, totalUniqueCommenters)

SELECT pages.pageId, pages.dateScanned, pages.likes, pages.talkingAbout,

SUM(commentTags.tag LIKE '%positive%')   AS positive, 
SUM(commentTags.tag LIKE '%negative%')   AS negative,

SUM(comments.gender = 'female')          AS females,
SUM(comments.gender = 'male')            AS males,

SUM(commentTags.tag = 'strong_positive') AS strongPositives,
SUM(commentTags.tag = 'positive')    AS positives,
SUM(commentTags.tag = 'weak_positive')   AS weakPositives,
SUM(commentTags.tag = 'neutral')         AS neutrals,
SUM(commentTags.tag = 'weak_negative')   AS weakNegatives,
SUM(commentTags.tag = 'negative')    AS negatives,
SUM(commentTags.tag = 'strong_negative') AS strongNegatives,

COUNT(DISTINCT statuses.statusId)    AS totalStatuses,
SUM(DISTINCT statuses.likesCount)    AS totalLikesCount,
SUM(DISTINCT statuses.sharesCount)       AS totalSharesCount,
COUNT(DISTINCT comments.commentId)   AS totalComments,
COUNT(DISTINCT comments.userName)        AS uniqueUsers

FROM pages
JOIN statuses ON pages.pageId = statuses.pageId AND pages.dateScanned = statuses.dateScanned
JOIN comments ON comments.statusID = statuses.statusId
JOIN commentTags ON comments.commentId = commentTags.commentId

WHERE pages.pageId = '115798033817' AND pages.dateScanned = '2013-11-05'

我尝试了ON DUPLICATE KEY UPDATE,这就是我进一步修改查询的方式

ON DUPLICATE KEY UPDATE 
totalData.pageId = pageId, totalData.dateScanned = dateScanned, 
totalData.totalPageLikes = totalPageLikes, totalData.totalTalkingAbout = totalTalkingAbout,
totalData.totalPos = positive, totalData.totalNeg = negative, totalData.totalFemales = females, 
totalData.totalMales = males, totalData.totalStrongPositives = strongPositives, 
totalData.totalPositives = positives, totalData.totalWeakPositives = weakPositives, 
totalData.totalNeutrals = neutrals, totalData.totalWeakNegatives = weakNegatives, 
totalData.totalNegatives = negatives, totalData.totalStrongNegatives = strongNegatives, 
totalData.totalStatuses = totalStatuses, totalData.totalStatusLikes = totalLikesCount, 
totalData.totalStatusShares = totalSharesCount, totalData.totalComments = totalComments, 
totalData.totalUniqueCommenters = uniqueUsers ;

但是当我运行查询时,它在字段列表中显示Unknown column 'positive'

1 个答案:

答案 0 :(得分:0)

知道了,我必须使用VALUES()函数,以便查询的ON DUPLICATE KEY UPDATE部分变得如下

ON DUPLICATE KEY UPDATE 
                        totalPageLikes = VALUES(totalPageLikes), totalTalkingAbout = VALUES(totalTalkingAbout), 
                        totalPos = VALUES(totalPos), totalNeg = VALUES(totalNeg), totalFemales = VALUES(totalFemales), totalMales = VALUES(totalMales), 
                        totalStrongPositives = VALUES(totalStrongPositives), totalPositives = VALUES(totalPositives), totalWeakPositives = VALUES(totalWeakPositives), 
                        totalNeutrals = VALUES(totalNeutrals), totalWeakNegatives = VALUES(totalWeakNegatives), totalNegatives = VALUES(totalNegatives), totalStrongNegatives = VALUES(totalStrongNegatives), 
                        totalStatuses = VALUES(totalStatuses), totalStatusLikes = VALUES(totalStatusLikes), totalStatusShares = VALUES(totalStatusShares), 
                        totalComments = VALUES(totalComments), totalUniqueCommenters = VALUES(totalUniqueCommenters) ";