Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
复杂的MySQL连接,包含3个表和多个关系_Mysql_Sql - Fatal编程技术网

复杂的MySQL连接,包含3个表和多个关系

复杂的MySQL连接,包含3个表和多个关系,mysql,sql,Mysql,Sql,我有3张表,包括个人、帐户和订阅。帐户属于个人,是一对多,帐户和订阅是多对多。我需要一个sql,可以从三个表中选择数据。- 人员表 +----------------------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------

我有3张表,包括个人、帐户和订阅。帐户属于个人,是一对多,帐户和订阅是多对多。我需要一个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 |
| 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    |                |
+-------------------+--------------+------+-----+---------+----------------+
订阅表

+-----------------+---------+------+-----+---------+----------------+
| 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 |
+--------+---------+-----------------+

试试这个。它可能正常工作…

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

这些表中没有任何东西可以将个人链接到帐户。除了这些,请提供有关表中数据的信息。@DaleM更新个人表。Thnx@DaleM,您的查询工作正常,但我没有清除确切的输出。countofperson表是36014,但您的查询返回我-24832。我想要所有属于个人的数据,如果订阅不可用,订阅将为空。有意义吗?更新预期输出。