1

我有以下 2 个表:

user_table:

user_id
job_id
job_link

job_table:

job_id
click_ip
click_date

我必须计算当前用户每个作业的不同点击次数和所有点击次数。谁能告诉我当前的表结构是否可行并对此有答案。谢谢。

我当前的查询也尝试了内部连接:

SELECT  `user_id` , job.job_id AS job_id,  `job_link`, 
    COUNT( job.click_ip ) AS click_ip
FROM  `job` ,  `user` 
WHERE job.job_id = user.job_id
AND user.user_id =7
GROUP BY job_id
4

1 回答 1

2

COUNT(DISTINCT field) 将完成这项工作:

SELECT user_id, 
    job.job_id AS job_id,
    job_link, 
    COUNT(job.click_ip) AS click_ip, 
    COUNT(DISTINCT job.click_ip) AS click_ip_distinct
FROM user JOIN job ON user.job_id = job.job_id
WHERE user.user_id =7
GROUP BY job_id

SQL 小提琴在这里:http ://sqlfiddle.com/#!2/5e03e/2/0

于 2013-09-08T21:30:34.470 回答