Jump to content


Photo

MySQL Join, possible?


  • Please log in to reply
4 replies to this topic

#1 mjlogan

mjlogan
  • Members
  • PipPipPip
  • Advanced Member
  • 122 posts

Posted 21 September 2006 - 01:33 PM

Hi all,

I just wondered if it was possible to combine the two querys below. At the moment the first query gets all active jobs and then the second query in the while, loops through the applications table counting the number of applicants.

So the query count for the page is, always 1 for the first query and then plus the number of jobs that are active, I was wondering if it's possible (and if so, could I please have some pointers) to combine the two querys so that is it only ever 1.

Thanks
Mark

$query = $connDatabase->openQuery("SELECT JOB_ID,JOB_TITLE,JOB_DATEREMOVED FROM tbljobs WHERE (JOB_STAFF='$userData[USERID]' AND JOB_DELETE=1) OR (JOB_STAFF='$userData[USERID]' AND JOB_ARCHIVE=1) ORDER BY JOB_DATEREMOVED DESC");


while($jobsData = $connDatabase->fetchArray($query)){
$appCountQ = $connDatabase->openQuery("SELECT COUNT(APP_ID) FROM tblapplications WHERE APP_JOBNUM='$jobsData[JOB_ID]'");
 $appCount = mysql_result($appCountQ, 0);
}



#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 22 September 2006 - 05:57 AM

Yes, you can..

SELECT JOB_ID,JOB_TITLE,JOB_DATEREMOVED FROM tbljobs WHERE (JOB_STAFF='$userData[USERID]' AND JOB_DELETE=1) OR (JOB_STAFF='$userData[USERID]' AND JOB_ARCHIVE=1) ORDER BY JOB_DATEREMOVED DESC
SELECT COUNT(APP_ID) FROM tblapplications WHERE APP_JOBNUM='$jobsData[JOB_ID]'

=>

SELECT JOB_ID, JOB_TITLE, JOB_DATEREMOVED, COUNT(*) AS APP_ID_COUNT
FROM tbljobs
JOIN tblapplications ON (tblapplications.APP_JOBNUM = tbljobs.JOB_ID)
WHERE (JOB_STAFF='$userData[USERID]' AND JOB_DELETE=1)
OR (JOB_STAFF='$userData[USERID]' AND JOB_ARCHIVE=1)
GROUP BY JOB_ID, JOB_TITLE, JOB_DATEREMOVED
ORDER BY JOB_DATEREMOVED DESC

It won't matter if you use count(*) or count(app_id).. if you want the count of distinct app_ids, then you need count(distinct app_id) in there ( I think that works with mysql.. i'm more used to postgres actually).

Anyway, the key thing there is you need not only a join, but also a group by.  A simple join won't work because you want to count the results from the second table.

#3 mjlogan

mjlogan
  • Members
  • PipPipPip
  • Advanced Member
  • 122 posts

Posted 22 September 2006 - 09:46 AM

Cool, Thank you very much. It was the group by that I had forgotten.

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 22 September 2006 - 10:08 AM

Just a few comments: you should always use a table prefix, because column names can and do collide; also, there is a difference between COUNT(*) and COUNT(colname), because the latter only counts non-NULL rows, not all rows, like the former.  In this case, since it's an INNER JOIN, it happens to make no difference.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 mjlogan

mjlogan
  • Members
  • PipPipPip
  • Advanced Member
  • 122 posts

Posted 22 September 2006 - 10:14 AM

Thanks,

Yes, I added the table prefixes in, it is something I always do, but I also make sure the column names do not collide. The query was almost spot on, but it was just returning no records.

A simple 'LEFT' resolved that.

Thanks again.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users