我有那三张桌子
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 |
+---------+-------------------------------------------+--------------------+
我想将下面的两个表结合起来,以便有一个包含职业描述、每个职位的帖子和每个职位的用户。
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 |
+-----------------+-------+
像这样的东西
+-------------------+-------+-------+
| job | Posts | Users |
+-------------------+-------+-------+
| Economics | 2 | 0 |
| Legal Advisors | 1 | 0 |
| Musicians | 1 | 1 |
| Plumbers | 1 | 0 |
| Software Engineer | 2 | 0 |
| Social Advisors | 0 | 1 |
+-------------------+-------+-------+
我基本上对您的两个查询执行了
occupation_field
表的左连接,通过 occuDscr 字段连接。这是您的新查询:如果您不清楚 USING 子句,这里是您的没有 USING 子句
这是您加载的示例数据:
这是第一个查询的执行
这是第二个查询的执行
试试看 !!!
更新 2012-01-11 11:50 EDT
要删除没有帖子的作业,请将第一个 LEFT JOIN 更改为 INNER JOIN:
要删除没有用户的作业,请将第二个 LEFT JOIN 更改为 INNER JOIN:
要删除没有用户或没有帖子的作业,请将两个 LEFT JOIN 更改为 INNER JOIN:
更新 2012-01-11 12:03 EDT
要删除没有 Post 或 Users 的 Jobs,请添加 WHERE 子句来检查其中一个是否大于零:
这应该是你所追求的(或者至少足以让你走上正轨):
occupation_field LEFT OUTER JOIN users ON usrOccuId = occuId
本质上是说“给我来自 的所有行occupation_field
,如果users
没有匹配的行,occuId
那么用NULL
值填充它”。另请参阅关于外部连接的维基百科文章。之后,我们在语句
NULL
中将值视为 0 ,这一切都完成了。CASE
SUM
编辑 @RolandoMySQLDBA 2012-01-11 11:42 EDT
嘿西蒙。我针对我创建的数据集运行了您的查询。
您的查询按已发布的方式工作。+1 !!!