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

发布于 2024-10-14 09:35:57 字数 1417 浏览 7 评论 0原文

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

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

任何帮助将不胜感激!

Basically I am trying to execute this query

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)
;

But I get this error:

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

Any help would be much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑红尘 2024-10-21 09:35:57

您可以使用 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 设置为来自同一用户的 MAXarticle_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;

You an use UPDATE (.. JOIN ..) SET syntax

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;

As a proof of concept for anyone else reading this, here is a table you can create and try the syntax on

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';

Now run the update (it sets the article_date of all records to the MAX article_date from the same user)

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;

Finally, inspect the contents

select * from user_news;
深海少女心 2024-10-21 09:35:57

如果您想执行此操作,则必须使用临时表。

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

You would have to use a temporary table if you want to do this.

I'd say it's time to have a think about what you're doing, why, and what the risks are :)

£冰雨忧蓝° 2024-10-21 09:35:57

表名

COMMUNICATION_RELEVANCE

是大写还是拼写错误?

the table name

COMMUNICATION_RELEVANCE

is it caps or typo?

宛菡 2024-10-21 09:35:57

基本数据库规范化表明表中的计算字段违反了规范化规则......您应该能够在需要时在查询中动态执行此计算。或者创建包含计算字段的视图。

basic database normalization would indicate that having a computed field in a table breaks the rules of normalization....you should just be able to perform this calculation in a query on the fly when you need it. Or create a view that contains the calculated field.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文