Jump to content

[SOLVED] Count query


zed420

Recommended Posts

Hi All

Can someone help me with creating a query please, I have three tables, User,job1,job2,job3 what I want is to know the count of each job a user has done. User table has primary key (id) all job tables have foreign key from User table (user_id). I just can not understand how to do it?

This is what I have so far but it doesn't give the result I want ??? ???

$query = "SELECT job1.job_id,job2.job_id,job3.job_id,
COUNT(*)
FROM job1,job2,job3,user
WHERE job1.user_id = user.id OR
job2.user_id = user.id OR
job3.user_id = user.id 
GROUP BY job_id";
$result = mysql_query($query) or die(mysql_error());
	$num_rows = mysql_num_rows($result);
echo "Normal:   $num_rows<p>";

Many thanks well in advance

Zed

 

Link to comment
Share on other sites

Don't worry fenway so was I... this is what I was looking for

 

SELECT id
     , ( SELECT COUNT(*)
           FROM job1
          WHERE user_id = user.id ) +
       ( SELECT COUNT(*)
           FROM job2
          WHERE user_id = user.id ) +
       ( SELECT COUNT(*)
           FROM job3
          WHERE user_id = user.id ) AS jobs_done
  FROM user

 

Thanks

Zed

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.