mysql用另一个表的计数更新一个表

时间:2015-03-01 01:22:47

标签: mysql-error-1054

如果你能提供帮助我会非常感激。 有两个表,customers和lead_report

我想从第一列获取计数并将它们放在第二列的匹配行中。

选择来源,计算(*)来自客户,其中leadource不为null和leadource!="" group by leadource;

select leadsource,count(*)  from customers where leadsource is not null  and leadsource !="" group by leadsource;

+---------------------+----------+
| leadsource          | count(*) |
+---------------------+----------+
| Show 2014           |        2 |
| Show 2013           |        4 |
| By Me               |        1 |
+---------------------+----------+


select source,leads from lead_report;

+---------------------+-------+    
| source              | leads |
+---------------------+-------+
| Show 2014           |     0 |
| Show 2013           |     0 |
| By Me               |     0 |
+---------------------+-------+



update lead_report leads inner join 
(select leadsource,count(*)  from customers where leadsource is not null    and leadsource !="" group by leadsource)
customers using (leadsource) set lead_report.leads =  customers.leadsource;
ERROR 1054 (42S22): Unknown column 'leadsource' in 'from clause'

1 个答案:

答案 0 :(得分:0)

update lead_report lr join 
(select leadsource, count(*) cnt from customers 
where leadsource is not null  and leadsource !="" group by leadsource) cust
ON lr.source=cust.leadsource
SET lr.leads=cust.cnt;