通过比较列来更新MYSQL表

时间:2017-01-29 18:47:37

标签: mysql

请我帮忙。 如果我的MYSQL表看起来像下图中的那个,例如:

Full Name     Customer _ Type Referrer 
Wayne Brown   Standard        Neil White 
Neil White    Standard        Wayne Brown 
Gordon Smith  Standard        Neil White 
Rukky Dick    Standard        Neil White 
Dorothy Ann   Standard        Tracy Cool 
Tracy Cool    Standard        Neil White 
Anthony Josh  Standard        Neil White 

如何编写UPDATE语句,将Neil White的客户类型更改为“Premium”,因为他已经推荐了5个或更多人?

我已经尝试过这段代码,但它没有给我结果。

Update customers set customer_type = “Premium” where full_name = (select referrer from customers having count(referrer) >= 5)

谢谢。

1 个答案:

答案 0 :(得分:0)

尝试执行以下操作:

UPDATE `your_table` as t1 
    INNER JOIN (select count(*) as `total` 
            from `your_table` as t2 
            where t2.`full_name_customer`= t1.`full_name_customer`
            group by t2.`referrer`
     ) as t3
 SET`full_name_customer`='Premium' 
 WHERE t3.total >= 5