I have those three tables
SELECT occuId as 'Occupation Id',occuDscr as 'Occupation Description' FROM occupation_field;
+---------------+------------------------+
| Occupation Id | Occupation Description |
+---------------+------------------------+
| 1 | Software Engineer |
| 2 | Economics |
| 3 | Structural Engineer |
| 4 | Legal Advisors |
| 5 | Plumbers |
| 6 | Social Advisors |
| 7 | Musicians |
+---------------+------------------------+
SELECT usrId AS 'User Id',usrName AS 'User Name',usrOccuId AS 'User Occupation Id' FROM users;
+---------+------------+--------------------+
| User Id | User Name | User Occupation Id |
+---------+------------+--------------------+
| 2 | goodys | 6 |
| 5 | realmadrid | 7 |
| 6 | petousis | 6 |
+---------+------------+--------------------+
SELECT pstId AS 'Post Id',pstTitle AS 'Post Title',pstOccuId AS 'Post Occupation Id' FROM job_post ORDER BY pstId;
+---------+-------------------------------------------+--------------------+
| Post Id | Post Title | Post Occupation Id |
+---------+-------------------------------------------+--------------------+
| 4 | Software Engineer Recruit | 1 |
| 5 | Web Developer Recruit | 1 |
| 6 | Saxophonist | 7 |
| 7 | Construction Company looking for plumber. | 5 |
| 8 | Economic Analyst | 2 |
| 9 | Legal Advisor | 4 |
| 10 | Economic Assistant | 2 |
+---------+-------------------------------------------+--------------------+
I want to combine the below two tables in order to have one with the Occupation Descriptions,Post per Job and Users per Job.
SELECT occuDscr job,count(pstOccuId) AS 'Posts' FROM job_post INNER JOIN occupation_field on pstOccuId = occuId GROUP BY job;
+-------------------+-------+
| job | Posts |
+-------------------+-------+
| Economics | 2 |
| Legal Advisors | 1 |
| Musicians | 1 |
| Plumbers | 1 |
| Software Engineer | 2 |
+-------------------+-------+
SELECT occuDscr job,count(usrOccuId) AS 'Users' FROM users INNER JOIN occupation_field on usrOccuId = occuId GROUP BY job;
+-----------------+-------+
| job | Users |
+-----------------+-------+
| Musicians | 1 |
| Social Advisors | 2 |
+-----------------+-------+
Something like this
+-------------------+-------+-------+
| job | Posts | Users |
+-------------------+-------+-------+
| Economics | 2 | 0 |
| Legal Advisors | 1 | 0 |
| Musicians | 1 | 1 |
| Plumbers | 1 | 0 |
| Software Engineer | 2 | 0 |
| Social Advisors | 0 | 1 |
+-------------------+-------+-------+
Best Answer
I essentially performed a LEFT JOIN of the
occupation_field
table against both of your queries, joining by means of the occuDscr field. Here is your new query:If the USING clause is not clear to you, here is your without the USING clause
Here is your sample data loaded:
Here is the execution of the first query
Here is the execution of the second query
Give it a Try !!!
UPDATE 2012-01-11 11:50 EDT
To remove Jobs with no Posts, change the first LEFT JOIN into INNER JOIN:
To remove Jobs with no Users, change the second LEFT JOIN into INNER JOIN:
To remove Jobs with no Users or no Posts, change the both LEFT JOINs into INNER JOINs:
UPDATE 2012-01-11 12:03 EDT
To remove Jobs with no Post or Users, add a WHERE clause to check if either cound is greater than zero: