2012-04-03 65 views
0

我正在嘗試編寫一個MySQL查詢,該查詢爲我提供組織名稱,其郵政編碼,屬於該組織的任何事件和該事件的郵政編碼的結果。我嘗試了各種各樣的加入,加入和選擇組合無濟於事。這是可能的嗎? (我可能對組織的地址和事件地址一個單獨的表,但現在看來似乎應該是可能只使用一個表)使用多個表連接或多個選擇來編寫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) 
+0

你在同一時間同時將與org_id和事項標識或者他們相互排斥的cc_addresses? – 2012-04-03 14:00:04

回答

2
select o.Name, oAddress.PostCode, e.Name, eAddress.PostCode 
from cc_organisations o 
inner join cc_addresses oAddress on oAddress.org_id = o.id 
left outer join cc_events e on e.org_id=o.id 
inner join cc_addresses eAddress on eAddress.event_id = e.id 
+0

可能需要檢查第一個連接上的isnull(oAddress.event_id),具體取決於是否可以在cc_addresses中同時設置event_id和org_id。 – 2012-04-03 13:58:45

+0

太棒了,謝謝。我甚至不知道外部連接,所以我會離開,並閱讀那些似乎是神奇的成分! – 2012-04-03 14:06:08

+0

作爲一個學習點,你的SQL和這個有什麼區別:SELECT o.name,o_address.post_code,e.name,e_address.post_code FROM cc_organisations AS o LEFT JOIN cc_addresses AS o_address LEFT OUTER JOIN cc_events AS e LEFT加入cc_addresses AS e_address ON o_address.org_id = o.id AND e.org_id = o.id AND e_address.org_id = e.id WHERE o.user_id = 1; – 2012-04-03 14:14:55

0
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; 

但我不是100%確定第二個查詢是否會失敗。

您的address表中包含郵政編碼;但它也有一個組織ID和事件ID外鍵。我們只需要檢查address表中的event_id,因爲任何事件都屬於組織。

  • 地址的事件匹配的事件ID
  • 事件的組織匹配的組織ID
+0

使用隱式內部聯接,您不會看到沒有事件的組織。此外,此查詢不會返回專門綁定到組織的地址,但不會返回事件。 (必須使用cc_addresses.org_id) – 2012-04-03 14:13:00