使用多个表联接或多个select编写MySQL查询

使用多个表联接或多个select编写MySQL查询,mysql,join,multiple-select-query,Mysql,Join,Multiple Select Query,我正在尝试编写一个MySQL查询,该查询提供组织名称、其邮政编码、属于该组织的任何事件以及该事件的邮政编码的结果。我试过各种各样的连接、连接和选择组合,但都没有用。这是可能的吗?(我可以为组织地址和事件地址创建一个单独的表,但似乎可以只使用一个表) 我的表格结构: mysql> DESCRIBE cc_organisations; +-------------+------------------+------+-----+---------+----------------+ | Fie

我正在尝试编写一个MySQL查询,该查询提供组织名称、其邮政编码、属于该组织的任何事件以及该事件的邮政编码的结果。我试过各种各样的连接、连接和选择组合,但都没有用。这是可能的吗?(我可以为组织地址和事件地址创建一个单独的表,但似乎可以只使用一个表)

我的表格结构:

mysql> DESCRIBE cc_organisations;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| user_id     | int(10) unsigned | NO   | MUL | NULL    |                |
| type        | enum('C','O')    | YES  |     | NULL    |                |
| name        | varchar(150)     | NO   | MUL | NULL    |                |
| description | text             | YES  |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql> DESCRIBE cc_events;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| org_id      | int(10) unsigned | NO   | MUL | NULL    |                |
| name        | varchar(150)     | NO   | MUL | NULL    |                |
| start_date  | int(11)          | NO   | MUL | NULL    |                |
| end_date    | int(11)          | YES  | MUL | NULL    |                |
| start_time  | int(11)          | NO   |     | NULL    |                |
| end_time    | int(11)          | NO   |     | NULL    |                |
| description | text             | YES  |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)

mysql> DESCRIBE cc_addresses;
+--------------+------------------+------+-----+---------+----------------+
| Field        | Type             | Null | Key | Default | Extra          |
+--------------+------------------+------+-----+---------+----------------+
| id           | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| org_id       | int(10) unsigned | YES  | MUL | NULL    |                |
| event_id     | int(10) unsigned | YES  | MUL | NULL    |                |
| post_code    | varchar(7)       | NO   | MUL | NULL    |                |
| address_1    | varchar(100)     | NO   |     | NULL    |                |
| address_2    | varchar(100)     | YES  |     | NULL    |                |
| town         | varchar(50)      | NO   |     | NULL    |                |
| county       | varchar(50)      | NO   |     | NULL    |                |
| email        | varchar(150)     | NO   |     | NULL    |                |
| phone        | int(11)          | YES  |     | NULL    |                |
| mobile       | int(11)          | YES  |     | NULL    |                |
| website_uri  | varchar(150)     | YES  |     | NULL    |                |
| facebook_uri | varchar(250)     | YES  |     | NULL    |                |
| twitter_uri  | varchar(250)     | YES  |     | NULL    |                |
+--------------+------------------+------+-----+---------+----------------+
14 rows in set (0.00 sec)
您可以更改您的排序和限制,我只是添加了这些,因为我不知道您的数据库有多大。。。你甚至可以逃脱:

SELECT cco.name as OrgName, cca.post_code as OrgPostCode, cce.*
FROM cc_events cce, cc_addresses cca, cc_organisations cco
WHERE  cca.event_id = cce.id AND cco.id=cce.org_id
ORDER BY cce.start_date LIMIT 50;
但是我不能100%确定第二个查询是否会失败

您的
地址
表中有邮政编码;但它也有一个组织id和事件id外键。我们只需要从
address
表中检查
事件\u id
,因为任何事件都属于一个组织

  • 地址的事件匹配事件ID
  • 事件的组织与组织ID匹配

可能需要在第一次加入时检查isnull(oAddress.event\u id),这取决于是否可以在cc\u addresses.Awesome中同时设置event\u id和org\u id。谢谢。我甚至不知道外部连接,所以我会离开并阅读这些,因为这似乎是神奇的成分!作为一个学习点,您的SQL与此有什么区别:选择o.name、o\u address.post\u code、e.name、,e_address.post_代码作为o LEFT JOIN cc_address作为o_address作为e LEFT JOIN cc_address作为e_address作为o_address.org_id=o.id和e.org_id=o.id和e_address.org_id=e.id,其中o.user_id=1;我相信在这个例子中你不需要左连接-对不起!尝试将左侧外部联接更改为内部联接。参赛者应重复组织中的记录,以记录赛事中的记录。您可以使用左连接在右have表中包含id为空的记录。Wiki应该在内部/左侧/右侧/外部连接方面对您有所帮助。您的SQL很难阅读,因为所有的where are at last on子句。我不建议你那样做。它们可能都是有效的sql,但最好将表联接的筛选器放在ON子句中。是同时分配org_id和event_id,还是它们在cc_地址中相互排斥?使用隐式内部联接,您不会看到没有事件的组织。此外,此查询不会返回特定于组织的地址,但不会返回事件。(必须使用cc_addresses.org_id)
 SELECT cco.name as OrgName, cca.post_code as OrgPostCode, cce.id,
        cce.org_id, cce.name, cce.start_date, cce.end_date, cce.start_time,
        cce.end_time, cce.description
 FROM   cc_events cce, cc_addresses cca, cc_organisations cco
 WHERE  cca.event_id = cce.id AND cco.id=cce.org_id 
 ORDER BY cce.start_date
 LIMIT 50;
SELECT cco.name as OrgName, cca.post_code as OrgPostCode, cce.*
FROM cc_events cce, cc_addresses cca, cc_organisations cco
WHERE  cca.event_id = cce.id AND cco.id=cce.org_id
ORDER BY cce.start_date LIMIT 50;