Jump to content

Archived

This topic is now archived and is closed to further replies.

mjlogan

MySQL Join, possible?

Recommended Posts

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

[code]$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);
}[/code]

Share this post


Link to post
Share on other sites
Yes, you can..

[code]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[/code]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.