具有可能的条件匹配的多表连接

时间:2016-02-19 04:42:14

标签: mysql

我有几个表,我在一个应用程序中组合,我在PHP中创建基本上创建一个检查列表。我意识到我可以使用PHP中的条件来解决这个问题,但我很好奇MySQL是否能够实现这一点,如果是这样,怎么样?具体来说,我有四个表,使用以下语句查询:

SELECT 
cl_status.status, 
users.user_first, 
cl_status.date AS status_date, 
cl_status.id AS status_id, 
cl_status.criteria_id,
cl_criteria.id AS cid, 
cl_criteria.description AS description 

FROM cl_criteria 
LEFT JOIN cl_lists 
  ON cl_criteria.cl_id = cl_lists.id 
RIGHT JOIN cl_status 
  ON cl_criteria.id = cl_status.criteria_id 
LEFT JOIN users 
  ON cl_status.user_id = users.user_id 
WHERE cl_lists.id = '1'

Table one - cl_lists:
+----+------------------+------------+------------+-------+
| id | title            | date       | comp_level | owner |
+----+------------------+------------+------------+-------+
|  1 | Newcomer's guide | 1452473606 |          1 |     1 |
+----+------------------+------------+------------+-------+

 Table two - cl_assign:
+----+-------+-------+------------+
| id | cl_id | owner | date       |
+----+-------+-------+------------+
|  1 |     1 |     1 | 1455843514 |
+----+-------+-------+------------+

Table three - cl_status:
+----+-------------+---------+-------------+------------+--------+----------+
| id | criteria_id | user_id | description | date       | status | comments |
+----+-------------+---------+-------------+------------+--------+----------+
|  2 |          66 |       1 |             |       NULL |      1 | NULL     |
| 15 |          65 |       1 |             | 1455842197 |      5 | NULL     |
| 16 |          67 |       1 |             | 1455842201 |      5 | NULL     |
| 17 |          68 |       1 |             | 1455842203 |      5 | NULL     |
| 18 |          69 |       1 |             | 1455842217 |      0 | NULL     |
| 19 |          70 |       1 |             | 1455842222 |      5 | NULL     |
| 20 |          72 |       1 |             | 1455842237 |      1 | NULL     |
| 21 |          71 |       1 |             | 1455842234 |      0 | NULL     |
| 22 |          73 |       1 |             | 1455842246 |      5 | NULL     |
| 23 |          76 |       1 |             | 1455842249 |      5 | NULL     |
| 24 |          77 |       1 |             | 1455842268 |      5 | NULL     |
| 25 |          78 |     152 |             | 1455854420 |      3 | NULL     |
| 26 |          81 |       1 |             | 1455843660 |      5 | NULL     |
+----+-------------+---------+-------------+------------+--------+----------+

Table four - users:
+---------+------------+
| user_id | user_first |
+---------+------------+
|       1 | Mark       |
|       2 | Test       |
+---------+------------+

理想情况下,我希望联接看起来像这样:

+--------+------------+-------------+-----------+-------------+------+-----------------------------+
| status | user_first | status_date | status_id | criteria_id | cid  | description                 |
+--------+------------+-------------+-----------+-------------+------+-----------------------------+
|      5 | Mark       |  1455842197 |        15 |          65 |   65 | Tour of facility            |
|      5 | Mark       |  1455842201 |        16 |          67 |   67 | Tax forms                   |
|      5 | Mark       |  1455842203 |        17 |          68 |   68 | 2 forms of ID               |
|      0 | Mark       |  1455842217 |        18 |          69 |   69 | Benefits                    |   |
|      5 | Mark       |  1455842246 |        22 |          73 |   73 | Intro to policies           |
|      5 | Mark       |  1455842249 |        23 |          76 |   76 | Setup email account         |
|   NULL | NULL       |  NULL       |      NULL |          78 |   78 | Setup Computer account      |
+--------+------------+-------------+-----------+-------------+------+-----------------------------+

然而,它看起来像这样:

+--------+------------+-------------+-----------+-------------+------+-----------------------------+
| status | user_first | status_date | status_id | criteria_id | cid  | description                 |
+--------+------------+-------------+-----------+-------------+------+-----------------------------+
|      5 | Mark       |  1455842197 |        15 |          65 |   65 | Tour of facility            |
|      5 | Mark       |  1455842201 |        16 |          67 |   67 | Tax forms                   |
|      5 | Mark       |  1455842203 |        17 |          68 |   68 | 2 forms of ID               |
|      0 | Mark       |  1455842217 |        18 |          69 |   69 | Benefits                    |
|      5 | Mark       |  1455842246 |        22 |          73 |   73 | Intro to policies           |
|      5 | Mark       |  1455842249 |        23 |          76 |   76 | Setup email account         |
|      3 | Temp       |  1455854420 |        25 |          78 |   78 | Setup Computer account      |
+--------+------------+-------------+-----------+-------------+------+-----------------------------+

有没有办法在加入之前应用条件?或者另一种方法来完成我想要的结果集?

修改 这是应用程序的截图: Application Screenshot

标准表将包括我拥有的每个清单的步骤。列表是各种清单的列表。状态表允许每个用户(例如Mark或Test)查看相同的清单并完成它,就像它是一个单独的文档一样。它还会填充该用户更新项目的日期/时间。

1 个答案:

答案 0 :(得分:1)

我怀疑您查询中的RIGHT JOIN会导致您要显示的记录被过滤掉。请注意,t1 RIGHT JOIN t2t2 LEFT JOIN t1相同,这意味着t1会丢失任何未出现在t2中的记录,t2会保留其所有记录。试试这个:

SELECT cl_status.status, users.user_first, cl_status.date AS status_date,
       cl_status.id AS status_id, cl_status.criteria_id, cl_criteria.id AS cid,
       cl_criteria.description AS description 
FROM cl_criteria LEFT JOIN cl_lists 
    ON cl_criteria.cl_id = cl_lists.id 
LEFT JOIN cl_status 
    ON cl_criteria.id = cl_status.criteria_id 
LEFT JOIN users 
    ON cl_status.user_id = users.user_id 
WHERE cl_lists.id = '1'