I know this is a little complicated and I could accomplish this using other methods, but please bear with me.
I am trying to join a table filled with dates with a table filled with events so I can show dates which have no events. I wrote a QUERY that handles this fine, but if I have more than one event on a particular date I can't seem to join multiple records. I have tried all variations of joins.
Please see my problem here
SELECT @RowNumber := @RowNumber + 1 AS DayNumber, D.Date, L.LessonID, L.Title
FROM dates D
JOIN (SELECT @RowNumber:= 0) R
LEFT JOIN lessons L ON L.DayNumber = (@RowNumber+1)
WHERE D.Date IN ('2012-01-01','2012-01-03','2012-01-05','2012-01-10')
ORDER BY DayNumber ASC LIMIT 0, 50
CREATE TABLE IF NOT EXISTS `dates` (
`DateID` int(11) NOT NULL AUTO_INCREMENT,
`Date` date NOT NULL,
`TimeStamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`DateID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `dates` (`DateID`, `Date`, `TimeStamp`) VALUES
(1, '2012-01-01', '2013-02-16 17:38:07'),
(2, '2012-01-02', '2013-02-16 17:38:07'),
(3, '2012-01-03', '2013-02-16 17:38:07'),
(4, '2012-01-04', '2013-02-16 17:38:07'),
(5, '2012-01-05', '2013-02-16 17:38:07'),
(6, '2012-01-06', '2013-02-16 17:38:07'),
(7, '2012-01-07', '2013-02-16 17:38:07'),
(8, '2012-01-08', '2013-02-16 17:38:07'),
(9, '2012-01-09', '2013-02-16 17:38:07'),
(10, '2012-01-10', '2013-02-16 17:38:07');
CREATE TABLE IF NOT EXISTS `lessons` (
`LessonID` int(11) NOT NULL AUTO_INCREMENT,
`DayNumber` int(11) NOT NULL DEFAULT '0',
`Title` varchar(1024) NOT NULL,
`TimeStamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`LessonID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `lessons` (`LessonID`, `DayNumber`, `Title`, `TimeStamp`) VALUES
(1, 1, 'asdfasdf', '2012-01-01 18:03:21'),
(2, 1, 'qwerqwer', '2012-01-05 18:03:21'),
(3, 3, '12341234', '2012-01-05 18:03:34');
Right now this returns this:
DAYNUMBER DATE LESSONID TITLE
1 January, 01 2012 00:00:00+0000 1 asdfasdf
2 January, 03 2012 00:00:00+0000 (null) (null)
3 January, 05 2012 00:00:00+0000 3 12341234
4 January, 10 2012 00:00:00+0000 (null) (null)
But I would like it to return this (note the second row on DayNumber 1 with the title "qwerqwer"):
DAYNUMBER DATE LESSONID TITLE
1 January, 01 2012 00:00:00+0000 1 asdfasdf
1 January, 01 2012 00:00:00+0000 1 qwerqwer
2 January, 03 2012 00:00:00+0000 (null) (null)
3 January, 05 2012 00:00:00+0000 3 12341234
4 January, 10 2012 00:00:00+0000 (null) (null)
If I'm understanding your question correctly, you could do something like this:
SELECT T.DayNumber,
T.Date,
L.LessonId,
L.Title
FROM (
SELECT @RowNumber := @RowNumber + 1 AS DayNumber,
D.Date
FROM dates D
JOIN (SELECT @RowNumber:= 0) R
WHERE D.Date IN ('2012-01-01','2012-01-03','2012-01-05','2012-01-10')
) T LEFT JOIN Lessons L ON
T.DayNumber = L.DayNumber
ORDER BY T.DayNumber ASC LIMIT 0, 50
Here is the updated Fiddle .
And here are the results:
DAYNUMBER DATE LESSONID TITLE
1 January, 01 2012 00:00:00+0000 1 asdfasdf
1 January, 01 2012 00:00:00+0000 2 qwerqwer
2 January, 03 2012 00:00:00+0000 (null) (null)
3 January, 05 2012 00:00:00+0000 3 12341234
4 January, 10 2012 00:00:00+0000 (null) (null)
BTW -- In your results above, you have Lesson Id 1 for your 2nd result -- I assume you meant Lesson Id 2 as in the above results.
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.