1

I have the following 2 tables:

user_table:

user_id
job_id
job_link

job_table:

job_id
click_ip
click_date

I have to count clicks both distinct and all clicks for each job for the current user. Can anybody tell me if this is possible with current table structure and have an answer to this. Thanks.

My current query is also tried with inner join:

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

1 Answer 1

2

COUNT(DISTINCT field) will do the job:

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 Fiddle here: http://sqlfiddle.com/#!2/5e03e/2/0

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.