简体   繁体   English

自加入和更新

[英]Self Join and update

I want to create a cron that do the following:我想创建一个执行以下操作的 cron:

check all user referrer col where referrer_id is 0, then Join with user email and update the referrer_id with the joined user id.检查referrer_id为0的所有用户referrer col,然后加入用户email并使用加入的用户ID更新referrer_id。 Here my code:这是我的代码:

SELECT us.id usId
     , us.referrer usRe
     , us.referrer_id usReId
     , re.id reId
     , re.email 
  from users us 
  LEFT 
  JOIN users re 
    ON us.referrer = re.email 

I have to finish the update part, the problem is that the Join give me wrong results, I get in all case the user id 1.我必须完成更新部分,问题是 Join 给了我错误的结果,我得到的所有情况下用户 id 1。

Did you find something wrong in that query?您是否在该查询中发现了问题?

Tnx肿瘤坏死因子

If I understand correctly:如果我理解正确:

update users u join
       users r
       on u.referrer = r.email 
    set u.referrer_id = r.id
    where referrer_id is null;

I guess this answer can work for you我想这个答案对你有用

UPDATE users as us INNER JOIN users as re 
ON us.referrer = re.email 
SET us.referrer_id = re.id 
WHERE us.referrer_id = 0;

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

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