mjlogan Posted September 21, 2006 Share Posted September 21, 2006 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.ThanksMark[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] Quote Link to comment Share on other sites More sharing options...
btherl Posted September 22, 2006 Share Posted September 22, 2006 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 DESCSELECT COUNT(APP_ID) FROM tblapplications WHERE APP_JOBNUM='$jobsData[JOB_ID]'=>SELECT JOB_ID, JOB_TITLE, JOB_DATEREMOVED, COUNT(*) AS APP_ID_COUNTFROM tbljobsJOIN 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_DATEREMOVEDORDER 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. Quote Link to comment Share on other sites More sharing options...
mjlogan Posted September 22, 2006 Author Share Posted September 22, 2006 Cool, Thank you very much. It was the group by that I had forgotten. Quote Link to comment Share on other sites More sharing options...
fenway Posted September 22, 2006 Share Posted September 22, 2006 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. Quote Link to comment Share on other sites More sharing options...
mjlogan Posted September 22, 2006 Author Share Posted September 22, 2006 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.