在Mysql中相互依赖加入三个表

时间:2018-05-17 15:15:13

标签: mysql database join mysql-5.7

我这里有三张桌子

1).organization_entries

+----+---------+-----------+-----------------+---------------+------+------------------+--------------+------------+---------+----------+--------------+---------------------+---------------------+---------------------+
| id | user_id | arena_ids | organization_id | serial_number | type | registration_fee | tax_group_id | from_date  | to_date | status   | comments     | created_at          | updated_at          | deleted_at          |
+----+---------+-----------+-----------------+---------------+------+------------------+--------------+------------+---------+----------+--------------+---------------------+---------------------+---------------------+
|  1 |       3 | [1]       |               1 |             1 |    1 |              600 |         NULL | 2018-02-04 | NULL    | inactive | present date | 2018-02-04 22:21:04 | 2018-05-13 14:09:46 | NULL                |
|  2 |       4 | [1]       |               1 |             2 |    1 |              600 |         NULL | 2018-02-05 | NULL    | active   | NULL         | 2018-02-04 22:22:29 | 2018-05-13 14:09:46 | NULL                |
|  3 |       5 | [1]       |               1 |             3 |    1 |              600 |         NULL | 2018-02-06 | NULL    | active   | NULL         | 2018-02-04 22:23:23 | 2018-05-13 14:09:46 | NULL                |
|  4 |       6 | [1]       |               1 |             4 |    1 |              600 |         NULL | 2018-02-03 | NULL    | inactive | NULL         | 2018-02-04 22:24:07 | 2018-05-13 14:09:46 | NULL                |
|  5 |       7 | [1]       |               1 |             5 |    1 |              600 |         NULL | 2017-12-05 | NULL    | active   | NULL         | 2018-02-04 22:25:33 | 2018-05-13 14:09:46 | NULL                |
|  6 |       8 | [1]       |               1 |             6 |    1 |              600 |         NULL | 2018-02-04 | NULL    | inactive | NULL         | 2018-02-04 22:32:22 | 2018-05-13 14:09:46 | NULL                |
|  7 |       9 | [2]       |               1 |             7 |    1 |              600 |         NULL | 2018-02-05 | NULL    | active   | NULL         | 2018-02-05 08:06:31 | 2018-05-13 14:09:46 | NULL                |
|  8 |      10 | null      |               1 |             8 |    1 |             NULL |         NULL | 2018-02-05 | NULL    | new      | NULL         | 2018-02-05 13:17:11 | 2018-05-13 14:09:46 | NULL                |
|  9 |      11 | [1]       |               1 |             9 |    1 |                0 |         NULL | 2018-02-05 | NULL    | active   | need to pay  | 2018-02-05 15:26:04 | 2018-05-13 14:09:46 | 2018-03-01 00:23:00 |
| 10 |      14 | [1]       |               1 |            10 |    1 |              600 |         NULL | 2018-02-10 | NULL    | active   | NULL         | 2018-02-10 12:40:30 | 2018-05-13 14:09:46 | NULL                |
+----+---------+-----------+-----------------+---------------+------+------------------+--------------+------------+---------+----------+--------------+---------------------+---------------------+---------------------+

2).coaching_class_entries

+----+---------+-------------------+---------------+------+------------+--------------+------------+------------+--------------+----------------+----------------+---------------------------------------------------+---------------------+---------------------+---------------------+
| id | user_id | coaching_class_id | serial_number | fees | bill_cycle | bill_plan_id | from_date  | to_date    | tax_group_id | last_bill_date | next_bill_date | comments                                          | created_at          | updated_at          | deleted_at          |
+----+---------+-------------------+---------------+------+------------+--------------+------------+------------+--------------+----------------+----------------+---------------------------------------------------+---------------------+---------------------+---------------------+
|  1 |       3 |                 3 |             1 | 5000 |          0 |            3 | 2018-02-04 | 2018-05-31 |         NULL | 2018-02-04     | NULL           | NULL                                              | 2018-02-04 22:25:55 | 2018-05-09 23:55:17 | 2018-02-18 16:10:01 |
|  2 |       3 |                 4 |             2 | 1000 |          2 |            4 | 2018-02-04 | 2018-05-09 |         NULL | 2018-02-04     | NULL           | NULL                                              | 2018-02-04 22:26:06 | 2018-05-09 23:55:17 | NULL                |
|  3 |       4 |                 3 |             3 | 5000 |          0 |            3 | 2018-02-05 | 2018-02-18 |         NULL | 2018-02-05     | NULL           | NULL                                              | 2018-02-04 22:26:22 | 2018-05-09 23:55:17 | NULL                |
|  4 |       4 |                 4 |             4 | 1000 |          2 |            4 | 2018-02-05 | NULL       |         NULL | 2018-02-05     | 2018-04-05     | NULL                                              | 2018-02-04 22:26:44 | 2018-05-09 23:55:17 | NULL                |
|  5 |       5 |                 3 |             5 | 5000 |          0 |            3 | 2018-02-06 | 2018-05-31 |         NULL | 2018-02-06     | NULL           | NULL                                              | 2018-02-04 22:27:19 | 2018-05-09 23:55:17 | NULL                |
|  6 |       5 |                 4 |             6 | 1000 |          2 |            4 | 2018-02-06 | 2018-04-28 |         NULL | 2018-02-06     | NULL           | NULL                                              | 2018-02-04 22:27:33 | 2018-05-09 23:55:17 | NULL                |
|  7 |       6 |                 3 |             7 | 5000 |          0 |            3 | 2018-02-03 | 2018-04-28 |         NULL | 2018-02-05     | NULL           | NULL                                              | 2018-02-04 22:27:50 | 2018-05-09 23:55:17 | NULL                |
|  8 |       6 |                 4 |             8 | 1000 |          2 |            4 | 2018-02-03 | 2018-04-28 |         NULL | 2018-02-04     | NULL           | NULL                                              | 2018-02-04 22:28:02 | 2018-05-09 23:55:17 | NULL                |
|  9 |       7 |                 3 |             9 | 5000 |          0 |            3 | 2017-12-03 | 2018-05-31 |         NULL | 2018-02-04     | NULL           | NULL                                              | 2018-02-04 22:28:31 | 2018-05-09 23:55:17 | NULL                |
| 10 |       7 |                 4 |            10 | 1000 |          2 |            4 | 2017-12-05 | 2018-08-04 |         NULL | 2018-02-05     | 2018-04-05     | starts on dec 5, 2017 and ends after 4 bill cycle | 2018-02-04 22:29:07 | 2018-05-09 23:55:17 | NULL                |
+----+---------+-------------------+---------------+------+------------+--------------+------------+------------+--------------+----------------+----------------+---------------------------------------------------+---------------------+---------------------+---------------------+

3)。 coaching_classes

+----+----------------------+-----------------+----------+------------+------+--------+--------+-------------+------------+------------+-----------+----------+----------+-------+------------+-------------------------------+-----------+---------------------+---------------------+---------------------+
| id | name                 | organization_id | arena_id | trainer_id | type | sports | gender | max_entries | from_date  | to_date    | from_time | to_time  | day_slot | fees  | bill_cycle | bill_plans                    | due_after | created_at          | updated_at          | deleted_at          |
+----+----------------------+-----------------+----------+------------+------+--------+--------+-------------+------------+------------+-----------+----------+----------+-------+------------+-------------------------------+-----------+---------------------+---------------------+---------------------+
|  1 | delete dummy plan    |               1 |        1 |       NULL | M    |      1 | NULL   |         100 | 2018-02-04 | NULL       | 11:11:00  | 10:10:00 |        2 | 10000 |          1 | [{"fees": 10000, "cycle": 1}] |        20 | 2018-02-04 15:40:21 | 2018-03-15 12:01:06 | 2018-02-04 22:17:18 |
|  2 | delete dummy plan 2  |               1 |        1 |       NULL | M    |      1 | NULL   |         100 | 2018-02-04 | NULL       | 10:10:00  | 11:11:00 |        2 |  2000 |          1 | [{"fees": 2000, "cycle": 1}]  |        10 | 2018-02-04 22:16:55 | 2018-03-15 12:01:06 | 2018-02-04 22:17:24 |
|  3 | Summer Camp          |               1 |        1 |       NULL | M    |      1 | NULL   |         100 | 2018-01-09 | 2018-05-31 | 10:10:00  | 11:11:00 |        2 |  5000 |          0 | [{"fees": 5000, "cycle": 0}]  |        20 | 2018-02-04 22:19:04 | 2018-03-25 14:52:01 | NULL                |
|  4 | Bi-Monthly Bad Plan  |               1 |        1 |       NULL | M    |      1 | NULL   |        NULL | 2018-01-01 | NULL       | 10:10:00  | 11:11:00 |       14 |  1000 |          2 | [{"fees": 1000, "cycle": 2}]  |        10 | 2018-02-04 22:19:44 | 2018-04-27 09:25:01 | NULL                |
|  5 | BadmintonEvent       |               1 |        1 |         10 | C    |      1 | NULL   |         200 | 2018-02-01 | 2018-02-08 | 07:50:00  | 08:50:00 |       14 |   500 |          0 | [{"fees": 500, "cycle": 0}]   |         2 | 2018-02-05 17:40:33 | 2018-05-08 12:42:25 | NULL                |
|  6 | Advanced Coaching    |               1 |        2 |         10 | C    |      1 | M      |         100 | 2018-02-06 | NULL       | 09:00:00  | 10:00:00 |       14 |  1200 |          1 | [{"fees": 1200, "cycle": 1}]  |        10 | 2018-02-06 12:17:40 | 2018-05-07 23:13:49 | NULL                |
|  7 | Advanced Coaching 2  |               1 |        2 |         10 | C    |      1 | M      |         100 | 2018-02-06 | NULL       | 09:00:00  | 10:00:00 |       14 |  1200 |          1 | [{"fees": 1200, "cycle": 1}]  |        10 | 2018-02-12 22:55:52 | 2018-04-28 16:41:43 | NULL                |
|  8 | Bi-Monthly good Plan |               1 |        1 |       NULL | M    |      1 | NULL   |        NULL | 2018-01-01 | NULL       | 10:10:00  | 11:11:00 |       14 |  1000 |          2 | [{"fees": 1000, "cycle": 2}]  |        10 | 2018-02-14 16:53:37 | 2018-03-15 12:01:06 | NULL                |
|  9 | One time             |               2 |        3 |       NULL | M    |      1 | NULL   |         100 | 2018-01-01 | 2018-04-30 | 06:00:00  | 07:00:00 |       31 |  2000 |          0 | [{"fees": 2000, "cycle": 0}]  |        10 | 2018-02-14 19:25:26 | 2018-03-15 12:01:06 | NULL                |
| 10 | Monthly              |               2 |        3 |       NULL | M    |      1 | NULL   |         100 | 2018-01-01 | 2018-04-30 | 06:00:00  | 07:00:00 |       31 |  2000 |          1 | [{"fees": 2000, "cycle": 1}]  |        10 | 2018-02-14 19:38:50 | 2018-05-02 18:52:49 | NULL                |
+----+----------------------+-----------------+----------+------------+------+--------+--------+-------------+------------+------------+-----------+----------+----------+-------+------------+-------------------------------+-----------+---------------------+---------------------+---------------------+

现在我想要的结果是这样的

基本上我的问题是我希望结果organization明智但orgnization_id位于coaching_classes

 +-----+--------+--------+----------------+-------------------------+-----------------------------+-------------------------------+-----------------------------+
| id  | userId | status | organizationId | coachingClassEntries.id | coachingClassEntries.userId | coachingClassEntries.fromDate | coachingClassEntries.toDate |
+-----+--------+--------+----------------+-------------------------+-----------------------------+-------------------------------+-----------------------------+
| 197 |    200 | active |              2 |                     252 |                         200 | 2018-05-17                    | NULL                        |
| 198 |    200 | active |              3 |                     252 |                         200 | 2018-05-17                    | NULL                        |
| 199 |    187 | new    |              3 |                    NULL |                        NULL | NULL                          | NULL                        |
| 200 |    199 | active |              2 |                     253 |                         199 | 2018-05-17                    | NULL                        |
| 201 |    199 | active |              4 |                     253 |                         199 | 2018-05-17                    | NULL                        |
| 202 |    194 | new    |              2 |                    NULL |                        NULL | NULL                          | NULL                        |
| 203 |    196 | active |              2 |                     246 |                         196 | 2018-05-13                    | NULL                        |
| 203 |    196 | active |              2 |                     254 |                         196 | 2018-05-17                    | NULL                        |
| 204 |    196 | active |              3 |                     246 |                         196 | 2018-05-13                    | NULL                        |
| 204 |    196 | active |              3 |                     254 |                         196 | 2018-05-17                    | NULL                        |
| 205 |    176 | new    |              2 |                    NULL |                        NULL | NULL                          | NULL                        |
| 206 |    176 | new    |              4 |                    NULL |                        NULL | NULL                          | NULL                        |
+-----+--------+--------+----------------+-------------------------+-----------------------------+-------------------------------+-----------------------------+

我写了类似这样的查询

  SELECT `organizationEntries`.*, `coachingClassEntries`.`id` AS `coachingClassEntries.id`, `coachingClassEntries`.`user_id` AS `coachingClassEntries.userId`, `coachingClassEntries`.`from_date` AS `coachingClassEntries.fromDate`, `coachingClassEntries`.`to_date` AS `coachingClassEntries.toDate` FROM (SELECT `organizationEntries`.`id`, `organizationEntries`.`user_id` AS `userId`, `organizationEntries`.`status`, `organizationEntries`.`organization_id` AS `organizationId` FROM `organization_entries` AS `organizationEntries` WHERE (`organizationEntries`.`deleted_at` > '2018-05-17 19:34:16' OR `organizationEntries`.`deleted_at` IS NULL) LIMIT 150, 200) AS `organizationEntries` LEFT OUTER JOIN `coaching_class_entries` AS `coachingClassEntries` ON `organizationEntries`.`userId` = `coachingClassEntries`.`user_id` AND (`coachingClassEntries`.`deleted_at` > '2018-05-17 19:34:16' OR `coachingClassEntries`.`deleted_at` IS NULL);

这是一个有点复杂的数据库,但我已经尝试了很多,但我无法得到正确的查询。请帮帮我或给我一些提示

0 个答案:

没有答案