根据其自己的表中的值更新列

时间:2011-01-25 18:06:56

标签: mysql sql sql-update mysql-error-1093

基本上我正在尝试执行此查询

UPDATE communication_relevance SET score = (SELECT ((ces.EXPERT_SCORE * cirm.CONSUMER_RATING) + (12.5 * scs.SIMILARITY)* (1 - EXP(-0.5 * (cal.TIPS_AMOUNT / AT.AVG_TIPS)) + .15))AS ANSWER_SCORE
FROM COMMUNICATION_RELEVANCE AS cr
JOIN network_communications AS nc
ON cr.COMMUNICATION_ID=nc.COMMUNICATIONS_ID
JOIN consumer_action_log AS cal
ON cr.ACTION_LOG_ID=cal.ACTION_LOG_ID
JOIN communication_interest_mapping AS cim
ON nc.PARENT_COMMUNICATIONS_ID=cim.COMMUNICATION_ID
JOIN consumer_interest_rating_mapping AS cirm
ON cr.CONSUMER_ID=cirm.CONSUMER_ID
    AND cim.CONSUMER_INTEREST_EXPERT_ID=cirm.CONSUMER_INTEREST_ID
JOIN consumer_expert_score AS ces
ON nc.SENDER_CONSUMER_ID=ces.CONSUMER_ID
    AND cim.CONSUMER_INTEREST_EXPERT_ID=CONSUMER_EXPERT_ID
JOIN survey_customer_similarity AS scs
ON cr.CONSUMER_ID=scs.CONSUMER_ID_2
    AND cal.SENDER_CONSUMER_ID=scs.CONSUMER_ID_1 
OR cr.CONSUMER_ID=scs.CONSUMER_ID_1
    AND cal.SENDER_CONSUMER_ID=scs.CONSUMER_ID_2
CROSS JOIN
(SELECT AVG(TIPS_AMOUNT) AS AVG_TIPS
FROM CONSUMER_ACTION_LOG
JOIN COMMUNICATION_RELEVANCE
ON CONSUMER_ACTION_LOG.SENDER_CONSUMER_ID=COMMUNICATION_RElEVANCE.consumer_id) AT)
;

但是我收到了这个错误:

Error:1/25/2011 1:03:20 PM 0:00:00.135: Lookup Error - MySQL Database Error: You can't specify target table 'communication_relevance' for update in FROM clause

非常感谢任何帮助!

4 个答案:

答案 0 :(得分:2)

你使用UPDATE(.. JOIN ..)SET语法

UPDATE communication_relevance X
JOIN (
    SELECT cr.COMMUNICATION_ID, ((ces.EXPERT_SCORE * cirm.CONSUMER_RATING)
        + (12.5 * scs.SIMILARITY)
        * (1 - EXP(-0.5 * (cal.TIPS_AMOUNT / AT.AVG_TIPS)) + .15)) AS ANSWER_SCORE
    FROM COMMUNICATION_RELEVANCE AS cr
    JOIN network_communications AS nc ON cr.COMMUNICATION_ID=nc.COMMUNICATIONS_ID
    JOIN consumer_action_log AS cal ON cr.ACTION_LOG_ID=cal.ACTION_LOG_ID
    JOIN communication_interest_mapping AS cim ON nc.PARENT_COMMUNICATIONS_ID=cim.COMMUNICATION_ID
    JOIN consumer_interest_rating_mapping AS cirm ON cr.CONSUMER_ID=cirm.CONSUMER_ID
        AND cim.CONSUMER_INTEREST_EXPERT_ID=cirm.CONSUMER_INTEREST_ID 
    JOIN consumer_expert_score AS ces ON nc.SENDER_CONSUMER_ID=ces.CONSUMER_ID
        AND cim.CONSUMER_INTEREST_EXPERT_ID=CONSUMER_EXPERT_ID
    JOIN survey_customer_similarity AS scs ON
        cr.CONSUMER_ID=scs.CONSUMER_ID_2 AND cal.SENDER_CONSUMER_ID=scs.CONSUMER_ID_1 
     OR cr.CONSUMER_ID=scs.CONSUMER_ID_1 AND cal.SENDER_CONSUMER_ID=scs.CONSUMER_ID_2
    CROSS JOIN
    (
        SELECT AVG(L.TIPS_AMOUNT) AS AVG_TIPS
        FROM CONSUMER_ACTION_LOG L
        JOIN COMMUNICATION_RELEVANCE R ON L.SENDER_CONSUMER_ID=R.consumer_id
    ) AT
) ON X.COMMUNICATION_ID = AT.COMMUNICATION_ID
SET X.score = AT.ANSWER_SCORE;

作为其他人阅读本文的概念证明,这是一个你可以创建并尝试语法的表

create table user_news(
    user_id int, article_id int, article_date timestamp,
    primary key(user_id, article_id));
insert into user_news select 1,2,'2010-01-02';
insert into user_news select 1,3,'2010-01-03';
insert into user_news select 1,4,'2010-01-01';
insert into user_news select 2,1,'2010-01-01';
insert into user_news select 2,2,'2010-01-02';
insert into user_news select 2,3,'2010-01-02';
insert into user_news select 2,4,'2010-01-02';
insert into user_news select 4,5,'2010-01-05';

现在运行更新(它将所有记录的article_date设置为来自同一用户的MAX article_date)

update user_news a
join (
  select b.user_id, max(b.article_date) adate
  from user_news b
  group by b.user_id) c
  on a.user_id=c.user_id
set a.article_date = c.adate;

最后,检查内容

select * from user_news;

答案 1 :(得分:0)

如果你想这样做,你必须使用临时表。

我想是时候考虑一​​下你在做什么,为什么以及风险是什么:)

答案 2 :(得分:0)

表名

COMMUNICATION_RELEVANCE

是封顶还是错字?

答案 3 :(得分:0)

基本数据库规范化将表明在表中具有计算字段会破坏规范化规则....您应该能够在需要时动态地在查询中执行此计算。或者创建一个包含计算字段的视图。