Jump to content

MySQL Join, possible?


mjlogan

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]

Link to comment
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.
Link to comment
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.
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.