[英]SQL query statement table joins
I have a problem with a SQL query and the resultset being returned not being what I expected. 我有一个SQL查询问题,返回的结果集不是我所期望的。
I have these three tables that I am trying to relate. 我有我要联系的这三个表。
events_detail 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 其中ID是主键
events_category_relationship 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 ID是主键
events_category_detail 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 ID是主键
What I need to do is grab only 1 event from each category and sorted by date of earliest appearance. 我需要做的是从每个类别中仅抓取1个事件,并按最早出现的日期进行排序。 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 我尝试过的SQL查询如下所示
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. 这仅连接3个表并按日期顺序返回结果。 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. 我尝试使用DISTINCT c.category_name和GROUP BY c.category_name,但是它们都不起作用。
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
. 您将需要使用子查询来获取每个name
和description
的min(start_date)
。 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: 然后,您将使用此结果events_details
回您的events_details
表,以获取与子查询中的数据关联的id
:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.