简体   繁体   中英

SQL query statement table joins

I have a problem with a SQL query and the resultset being returned not being what I expected.

I have these three tables that I am trying to relate.

events_detail

 __________________
| ID | start_date |
| 1  | 2012-08-09 |
| 2  | 2013-02-13 |
| 3  | 2012-12-12 |
| 4  | 2013-01-21 |
| 5  | 2012-12-25 |
-------------------

where ID is the primary key

events_category_relationship

__________________________    
| ID | event_id | cat_id |
| 1  |     1    |    1   |
| 2  |     2    |    4   |
| 3  |     3    |    2   |
| 4  |     4    |    2   |
| 5  |     5    |    3   |
--------------------------

where ID is primary key

events_category_detail

 __________________________________
| ID | name   | description        | 
| 1  | Europe | Kings and castles! |
| 2  | USA    | Freedoms           |
| 3  | China  | Made in China      |
| 4  | UK     | Big Brother        |
------------------------------------

where ID is primary key

What I need to do is grab only 1 event from each category and sorted by date of earliest appearance. So what I should expect in my result is the following

Result Set

________________________________________________________________
| e_id | start_date | c_id | category_name | category_desc      |
|   1  | 2012-08-09 |   1  |     Europe    | Kings and castles! |
|   3  | 2012-12-12 |   2  |     USA       | Freedoms           |
|   5  | 2012-12-25 |   3  |     China     | Made in China      |
|   2  | 2013-02-13 |   4  |     UK        | Big Brother        |
-----------------------------------------------------------------

My SQL query that I tried looks like this

SELECT e.id, e.start_date, c.category_name, c.category_desc
FROM events_detail e
JOIN events_category_relationship r ON r.event_id = e.id
JOIN events_category_detail c ON c.id = r.cat_id
ORDER BY date(e.start_date)

This just joins the 3 tables and returns the result in order by date. What I am stuck on is making it so that only one of each category is displayed like the desired result set above. I have tried using DISTINCT c.category_name and GROUP BY c.category_name, but none of them works.

Any help or advice will be greatly appreciated.

You will want to use a subquery to get the min(start_date) for each name and description . You will then use this result to join back to your events_details table to get the id associated with the data in the subquery:

SELECT e.id, 
  d.start_date, 
  d.name, 
  d.description
FROM events_detail e
INNER JOIN
(
  select min(e.start_date) Start_date, 
    c.name, 
    c.description
  from events_detail e
  INNER JOIN events_category_relationship r 
    ON r.event_id = e.id
  INNER JOIN events_category_detail c 
    ON c.id = r.cat_id
  group by c.name, c.description
)  d
  on e.start_date = d.Start_date
ORDER BY date(e.start_date)

See SQL Fiddle with Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM