我正在尝试计算用户在任何特定时间将拥有的潜在客户,但我收到错误MySQL返回空结果集(即零行)。但是我在users表中以及在lead表中都有数据。
表名:用户(user_id是主键)
+----------+
| user_id |
+----------+
| 1 |
| 2 |
| 3 |
+----------+
表名:lead(id是主键,user_id是外键)
+-----------+----------+
| id | user_id |
+-----------+----------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+-----------+----------+
这是我的代码:
function user_leads_count() {
return mysql_result(mysql_query("SELECT users.user_id, COUNT(leads.id) AS NumberOfLeads FROM (leads INNER JOIN users ON leads.id=users.user_id) GROUP BY id HAVING COUNT(leads.id) > 0"));
}
答案 0 :(得分:0)
试试这个SQL
SELECT users.user_id, COUNT(leads.id) AS NumberOfLeads
FROM leads
JOIN users USING (user_id)
GROUP by user
HAVING NumberOfLeads > 0
答案 1 :(得分:0)
您是否需要引用users表,如果没有尝试这应该工作
SELECT COUNT(id) AS NumberOfLeads, user_id
FROM Leads
GROUP BY user_id
HAVING COUNT(leads.id) > 0