复杂的MySQL加入3个表和many2many关系

时间:2014-11-14 05:54:35

标签: mysql sql

我有3个人,帐户和订阅。帐户属于Person一对多,帐户和订阅是多对多的。我需要一个可以从三个表中选择数据的SQL .-

人员表

+----------------------------+--------------+------+-----+---------+----------------+
| Field                      | Type         | Null | Key | Default | Extra          |
+----------------------------+--------------+------+-----+---------+----------------+
| id                         | int(11)      | NO   | PRI | NULL    | auto_increment |
| username                   | varchar(128) | NO   |     | NULL    |                |
| password                   | varchar(128) | NO   |     | NULL    |                |
| account_id                 | int(11)      | NO   |     | NULL    |                |
+----------------------------+---------------+------+---------------+----------------+

帐户表

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |

+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| yearly_renew | tinyint(1)   | NO   |     | NULL    |                |
| date_joined  | date         | NO   |     | NULL    |                |
| last_renewed | datetime     | YES  |     | NULL    |                |
| signup       | tinyint(1)   | NO   |     | NULL    |                |
| reason       | varchar(100) | YES  |     | NULL    |                |
| yearly_total | int(11)      | YES  |     | NULL    |                |
| total        | int(11)      | YES  |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+

帐户订阅表

+-----------------+---------+------+-----+---------+----------------+
| Field           | Type    | Null | Key | Default | Extra          |
+-----------------+---------+------+-----+---------+----------------+
| id              | int(11) | NO   | PRI | NULL    | auto_increment |
| account_id      | int(11) | NO   | MUL | NULL    |                |
| subscription_id | int(11) | NO   | MUL | NULL    |                |
+-----------------+---------+------+-----+---------+----------------+

订阅表

+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| id                | int(11)      | NO   | PRI | NULL    | auto_increment |
| subscription_type | varchar(150) | YES  |     | NULL    |                |
| hold              | tinyint(1)   | NO   |     | NULL    |                |
| start_date        | date         | YES  |     | NULL    |                |
| end_date          | date         | YES  |     | NULL    |                |
| amount_paid       | double       | YES  |     | NULL    |                |
| date_paid         | date         | YES  |     | NULL    |                |
| transaction_id    | int(11)      | YES  |     | NULL    |                |
| free              | tinyint(1)   | NO   |     | NULL    |                |
+-------------------+--------------+------+-----+---------+----------------+

在单个查询中期望输出 -

输出

+-----------+------------+--------------+
| person.id | account.id | subscription.id |
+-----------+------------+--------------+
|     10    |      11 |           20 |
|     15    |      32 |           45 |
|     23    |      43 |         null |
+--------+---------+-----------------+

2 个答案:

答案 0 :(得分:2)

试试这个。它可以正常工作......

SELECT  p.Id AS person.id
       ,p.account_id AS account.id
       ,acsub.subscription_id as subscription.id
FROM   Person AS p
       LEFT JOIN Account-Subscription AS acsub ON p.account_id=acsub.account_id

答案 1 :(得分:0)

SELECT  p.Id AS person.id
       ,p.account_id AS account.id
       ,acsub.subscription_id as subscription.id
FROM   Person AS p
       INNER JOIN
       Account-Subscription AS acsub ON p.account_id=acsub.account_id