is it ok to do this (and it works)
If it works.... who am I to argue.
However, if you would have asked if this was the best way... I would have to say, "It's pretty good, but can be better". You see, every time you 'go to the database', there is going to be a penalty for it. That penalty is performance. Now, understand that I'm NOT suggesting it's going to take a long time. And it's entirely possible that what I about to suggest will actually be slower, but it's something to consider as you move forward with the development.
First, let's take a look at the 2 queries you have.
[tt]
SELECT COUNT(*) As OpenJobCount
FROM tynansdb.tynan_jobs
WHERE job_status = 'open'
SELECT COUNT(*) As ClosedJobCount
FROM tynansdb.tynan_jobs
WHERE engineer_name = 'Joe'
[/tt]
Notice how the 2 queries are very similar. In fact, the only difference is in the where clause. As such, the 2 queries can be combined in to one query instead. Like this...
Code:
Select Sum(Case When Job_Status = 'open' Then 1 Else 0 End) as OpenJobCount,
Sum(Case When engineer_name = 'Joe' Then 1 Else 0 End) As ClosedJobCount
FROM tynansdb.tynan_jobs
Summing 1 is the same as count, so you can get multiple 'Counts' from the same query.
I said earlier that this method may actually be slower. And it's true. You'll have to test the performance to know. You see, when you have a WHERE clause, the SQL engine can make effective use of indexes, which would make the query faster. Since each count has a different filter condition, the separate queries could each use their own index. Combined, there is no where clause, and therefore, no indexes would be used. Of course, if you don't already have indexes on the Engineer_Name column or the job_status column, then the individual queries wouldn't be using indexes either, so the combined query may be faster.
Of course, there are other ways that you can combine the query (using sql) so that indexes would be used.
Code:
Select (
SELECT COUNT(*)
FROM tynansdb.tynan_jobs
WHERE job_status = 'open'
) As OpenJobCount,
(
SELECT COUNT(*)
FROM tynansdb.tynan_jobs
WHERE engineer_name = 'Joe'
) As ClosedJobCount
This query uses sub-queries to calculate the values. Since each query is left 'as-is', each sub query would be able to use indexes to return the values faster.
Please understand that the difference in execution time may be VERY minimal. In my opinion, this is no excuse for inaction. You see, as tables get larger, the performance becomes more important. Also, as the number of users increases, it's more important to have faster performance. All of this leads to scalability. I encourage you to make the effort to understand the techniques I describe here. This particular page you are working on may not benefit from these techniques. It's better for you to understand them now, so that you can apply these techniques to other queries/pages you may need to work on later.
-George
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom