eu tenho essas tres mesas
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 |
+---------+-------------------------------------------+--------------------+
Eu quero combinar as duas tabelas abaixo para ter uma com as Descrições de Ocupação, Cargo por Cargo e Usuários por Cargo.
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 |
+-----------------+-------+
Algo assim
+-------------------+-------+-------+
| job | Posts | Users |
+-------------------+-------+-------+
| Economics | 2 | 0 |
| Legal Advisors | 1 | 0 |
| Musicians | 1 | 1 |
| Plumbers | 1 | 0 |
| Software Engineer | 2 | 0 |
| Social Advisors | 0 | 1 |
+-------------------+-------+-------+
Eu essencialmente executei um LEFT JOIN da
occupation_field
tabela em ambas as suas consultas, unindo por meio do campo occuDscr. Aqui está sua nova consulta:Se a cláusula USING não estiver clara para você, aqui está sua cláusula sem a cláusula USING
Aqui estão seus dados de amostra carregados:
Aqui está a execução da primeira consulta
Aqui está a execução da segunda consulta
De uma chance !!!
ATUALIZAÇÃO 2012-01-11 11:50 EDT
Para remover trabalhos sem postagens, altere o primeiro LEFT JOIN para INNER JOIN:
Para remover trabalhos sem usuários, altere o segundo LEFT JOIN para INNER JOIN:
Para remover trabalhos sem usuários ou postagens, altere os dois LEFT JOINs para INNER JOINs:
ATUALIZAÇÃO 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:
This should be what you're after (or at least enough to set you on the right track):
occupation_field LEFT OUTER JOIN users ON usrOccuId = occuId
is essentially saying "give me all rows fromoccupation_field
, and ifusers
doesn't have a matching row for thisoccuId
then fill it in withNULL
values". See also the wikipedia article on outer joins.After that, we treat
NULL
values as 0 in theCASE
statement,SUM
it all and we're done.Edit By @RolandoMySQLDBA 2012-01-11 11:42 EDT
Hey Simon. I ran your query against the dataset I made.
Your query works as published. +1 !!!