Frage

I am trying to write a MySQL query that gives me results of Organisation Name, its Post Code, any Events that belong to the Organisation and the Post Code of that Event. I've tried all sorts of of join, join and select combinations to no avail. Is this something that is possible ? (I could have a separate table for Org Address and Event Address but it seems like it should be possible to use just one table)

My table structures:

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)
War es hilfreich?

Lösung

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

Andere Tipps

 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;

You can change your sort and limit, I just added those in because I don't know how big your DB is... You may even be able to get away with:

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;

But im not 100% sure if the 2nd query will bum out or not.

Your address table has the post codes in it; but it also has an organization id and event id foreign keys. We only need to check the event_id from the address table because any event will belong to an organization.

  • Address's Event matched Event ID
  • Event's Organization matched Organization ID
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top