Please help whith query. Have a table:
id | u_id | start_date | end_date |
-------------------------------------
01 | 7 | 2012-09-05 | 2012-09-23 |
02 | 4 | 2012-09-10 | 2012-09-15 |
03 | 4 | 2012-09-27 | 2012-10-05 |
04 | 5 | 2012-10-01 | 2012-10-09 |
05 | 4 | 2012-10-10 | 2012-10-15 |
06 | 7 | 2012-10-23 | 2012-11-05 |
07 | 5 | 2012-11-05 | 2012-11-12 |
08 | 4 | 2012-11-08 | 2012-11-10 |
I want to select all records where month=10 and max “start_date” from prev month and min “end_date” from next month.
Needed as a result
id | u_id | start_date | end_date |
-------------------------------------
03 | 4 | 2012-09-27 | 2012-10-05 |
04 | 5 | 2012-10-01 | 2012-10-09 |
05 | 4 | 2012-10-10 | 2012-10-15 |
06 | 7 | 2012-10-23 | 2012-11-05 |
I don’t understand what i do wrong with this query
SELECT start_date, end_date
FROM _xata_owner
INNER JOIN (
SELECT max(start_date) AS date FROM _xata_owner
GROUP BY DATE_FORMAT(start_date, "%m")
) b ON month(b.date)=09
WHERE month(start_date)=10
ORDER BY start_date
Thanks!
So you want:
Get records where the month in start_date is 10. For that:
SELECT id, u_id, start_date, end_date
FROM _xata_owner
WHERE month(start_date)=10
Get the max “end_date” where the “start_date” is in the previous month:
SELECT id, u_id, start_date, end_date
FROM _xata_owner
WHERE month(start_date) = 09 AND end_date =
(SELECT MAX(end_date) FROM _xata_owner WHERE month(start_date) = 09)
Get the min “end_date” where the “end_date” is in the next month:
SELECT id, u_id, start_date, end_date
FROM _xata_owner
WHERE month(end_date) = 11 AND end_date =
(SELECT MIN(end_date) FROM _xata_owner WHERE month(end_date) = 11)
Put them all together:
This sounds like what you want anyway. Your question is a little confusing.