blimey this is a bit more complicated than i thought, no wonder i had trouble with it. thanks very much for the answer. it’s not quite working though – nearly is though. for most (but not all) pub_datetime’s only one date is given. but i realise the info i provided wasn’t enough, so, all info:
the table:
CREATE TABLE IF NOT EXISTS daTable (
pub_datetime datetime NOT NULL,
`date` date NOT NULL,
score tinyint,
PRIMARY KEY (pub_datetime,`date`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
data:
INSERT INTO `daTable` (`pub_datetime`, `date`, `score`) VALUES
('2010-05-18 19:50:00', '2010-05-19', 27),
('2010-05-18 19:50:00', '2010-05-20', 26),
('2010-05-18 19:50:00', '2010-05-21', 24),
('2010-05-19 01:31:00', '2010-05-19', 27),
('2010-05-19 01:31:00', '2010-05-20', 24),
('2010-05-19 01:31:00', '2010-05-21', 28),
('2010-05-19 07:35:00', '2010-05-19', 27),
('2010-05-19 07:35:00', '2010-05-20', 25),
('2010-05-19 07:35:00', '2010-05-21', 28),
('2010-05-19 19:50:00', '2010-05-20', 25),
('2010-05-19 19:50:00', '2010-05-21', 28),
('2010-05-19 19:50:00', '2010-05-22', 24),
('2010-05-20 01:31:00', '2010-05-20', 26),
('2010-05-20 01:31:00', '2010-05-21', 26),
('2010-05-20 01:31:00', '2010-05-22', 28),
('2010-05-20 07:35:00', '2010-05-20', 26),
('2010-05-20 07:35:00', '2010-05-21', 26),
('2010-05-20 07:35:00', '2010-05-22', 28),
('2010-05-20 19:45:00', '2010-05-21', 26),
('2010-05-20 19:45:00', '2010-05-22', 28),
('2010-05-20 19:45:00', '2010-05-23', 28),
('2010-05-21 01:31:00', '2010-05-21', 26),
('2010-05-21 01:31:00', '2010-05-22', 24),
('2010-05-21 01:31:00', '2010-05-23', 28),
('2010-05-21 07:35:00', '2010-05-23', 28),
('2010-05-21 07:35:00', '2010-05-22', 24),
('2010-05-21 07:35:00', '2010-05-21', 26),
('2010-05-21 10:46:00', '2010-05-21', 23),
('2010-05-21 10:46:00', '2010-05-22', 26),
('2010-05-21 10:46:00', '2010-05-23', 29),
('2010-05-21 19:50:00', '2010-05-22', 26),
('2010-05-21 19:50:00', '2010-05-23', 29),
('2010-05-21 19:50:00', '2010-05-24', 26),
('2010-05-22 01:31:00', '2010-05-22', 28),
('2010-05-22 01:31:00', '2010-05-23', 31),
('2010-05-22 01:31:00', '2010-05-24', 24),
('2010-05-22 07:35:00', '2010-05-22', 28),
('2010-05-22 07:35:00', '2010-05-23', 31),
('2010-05-22 07:35:00', '2010-05-24', 24),
('2010-05-22 19:55:00', '2010-05-23', 31),
('2010-05-22 19:55:00', '2010-05-24', 24),
('2010-05-22 19:55:00', '2010-05-25', 23),
('2010-05-23 01:31:00', '2010-05-23', 27),
('2010-05-23 01:31:00', '2010-05-24', 24),
('2010-05-23 01:31:00', '2010-05-25', 19),
('2010-05-23 07:35:00', '2010-05-23', 27),
('2010-05-23 07:35:00', '2010-05-24', 24),
('2010-05-23 07:35:00', '2010-05-25', 19),
('2010-05-23 16:50:00', '2010-05-23', 27),
('2010-05-23 16:50:00', '2010-05-24', 24),
('2010-05-23 16:50:00', '2010-05-25', 19),
('2010-05-23 19:50:00', '2010-05-24', 24),
('2010-05-23 19:50:00', '2010-05-25', 19),
('2010-05-23 19:50:00', '2010-05-26', 17),
('2010-05-24 01:30:00', '2010-05-24', 22),
('2010-05-24 01:30:00', '2010-05-25', 10),
('2010-05-24 01:30:00', '2010-05-26', 17),
('2010-05-24 07:35:00', '2010-05-24', 22),
('2010-05-24 07:35:00', '2010-05-25', 10),
('2010-05-24 07:35:00', '2010-05-26', 17),
('2010-05-24 19:50:00', '2010-05-25', 10),
('2010-05-24 19:50:00', '2010-05-26', 17),
('2010-05-24 19:50:00', '2010-05-27', 19),
('2010-05-25 01:31:00', '2010-05-25', 10),
('2010-05-25 01:31:00', '2010-05-26', 19),
('2010-05-25 01:31:00', '2010-05-27', 20),
('2010-05-25 07:35:00', '2010-05-25', 10),
('2010-05-25 07:35:00', '2010-05-26', 19),
('2010-05-25 07:35:00', '2010-05-27', 20),
('2010-05-25 14:50:00', '2010-05-25', 17),
('2010-05-25 14:50:00', '2010-05-26', 19),
('2010-05-25 14:50:00', '2010-05-27', 20),
('2010-05-25 19:50:00', '2010-05-26', 19),
('2010-05-25 19:50:00', '2010-05-27', 20),
('2010-05-25 19:50:00', '2010-05-28', 18),
('2010-05-26 01:31:00', '2010-05-26', 19),
('2010-05-26 01:31:00', '2010-05-27', 17),
('2010-05-26 01:31:00', '2010-05-28', 16),
('2010-05-26 07:35:00', '2010-05-26', 19),
('2010-05-26 07:35:00', '2010-05-27', 15),
('2010-05-26 07:35:00', '2010-05-28', 16),
('2010-05-26 19:45:00', '2010-05-27', 16),
('2010-05-26 19:45:00', '2010-05-28', 16),
('2010-05-26 19:45:00', '2010-05-29', 11),
('2010-05-27 01:31:00', '2010-05-27', 22),
('2010-05-27 01:31:00', '2010-05-28', 21),
('2010-05-27 01:31:00', '2010-05-29', 16),
('2010-05-27 07:35:00', '2010-05-27', 23),
('2010-05-27 07:35:00', '2010-05-28', 21),
('2010-05-27 07:35:00', '2010-05-29', 14),
('2010-05-27 19:40:00', '2010-05-28', 21),
('2010-05-27 19:40:00', '2010-05-29', 14),
('2010-05-27 19:40:00', '2010-05-30', 17),
('2010-05-28 01:31:00', '2010-05-28', 18),
('2010-05-28 01:31:00', '2010-05-29', 14),
('2010-05-28 01:31:00', '2010-05-30', 18),
('2010-05-28 05:50:00', '2010-05-28', 18),
('2010-05-28 05:50:00', '2010-05-29', 14),
('2010-05-28 05:50:00', '2010-05-30', 18),
('2010-05-28 07:35:00', '2010-05-28', 18),
('2010-05-28 07:35:00', '2010-05-29', 15),
('2010-05-28 07:35:00', '2010-05-30', 18),
('2010-05-28 19:40:00', '2010-05-29', 16),
('2010-05-28 19:40:00', '2010-05-30', 18),
('2010-05-28 19:40:00', '2010-05-31', 16),
('2010-05-29 01:31:00', '2010-05-29', 12),
('2010-05-29 01:31:00', '2010-05-30', 19),
('2010-05-29 01:31:00', '2010-05-31', 19),
('2010-05-29 07:35:00', '2010-05-29', 11),
('2010-05-29 07:35:00', '2010-05-30', 19),
('2010-05-29 07:35:00', '2010-05-31', 19),
('2010-05-29 19:45:00', '2010-05-30', 19),
('2010-05-29 19:45:00', '2010-05-31', 18),
('2010-05-29 19:45:00', '2010-06-01', 18),
('2010-05-30 01:31:00', '2010-05-30', 18),
('2010-05-30 01:31:00', '2010-05-31', 15),
('2010-05-30 01:31:00', '2010-06-01', 15),
('2010-05-30 07:35:00', '2010-05-30', 18),
('2010-05-30 07:35:00', '2010-05-31', 15),
('2010-05-30 07:35:00', '2010-06-01', 18),
('2010-05-30 14:50:00', '2010-05-30', 18),
('2010-05-30 14:50:00', '2010-05-31', 15),
('2010-05-30 14:50:00', '2010-06-01', 18),
('2010-05-30 19:40:00', '2010-05-31', 15),
('2010-05-30 19:40:00', '2010-06-01', 16),
('2010-05-30 19:40:00', '2010-06-02', 25),
('2010-05-31 02:21:00', '2010-05-31', 15),
('2010-05-31 02:21:00', '2010-06-01', 20),
('2010-05-31 02:21:00', '2010-06-02', 23),
('2010-05-31 03:20:00', '2010-05-31', 15),
('2010-05-31 03:20:00', '2010-06-01', 20),
('2010-05-31 03:20:00', '2010-06-02', 23),
('2010-05-31 05:51:00', '2010-05-31', 15),
('2010-05-31 05:51:00', '2010-06-01', 19),
('2010-05-31 05:51:00', '2010-06-02', 23),
('2010-05-31 07:35:00', '2010-05-31', 15),
('2010-05-31 07:35:00', '2010-06-01', 18),
('2010-05-31 07:35:00', '2010-06-02', 23);
the sql query results in (printed out using php, including making the dates relative):
pub_datetime date score
----------------------------------------------
Wed 19th May 7.35am Wed 19th May 28
Thu 20th May 7.35am Thu 20th May 27
Fri 21st May 10.46am Fri 21st May 24
Sat 22nd May 7.35am Sat 22nd May 29
Sun 23rd May 4.50pm Sun 23rd May 28
Mon 24th May 7.35am Mon 24th May 23
Tuesday 2.50pm Tuesday 18
Wednesday 7.35am Wednesday 20
Thursday 7.35am Thursday 24
Friday 7.35am Friday 19
Saturday 7.35am Saturday 12
Yesterday 2.50pm Yesterday 19
Today 7.35am Today 16
Tomorrow 19
Wednesday 24
edit: just to make clear, those last three rows all
have the pub_datetime of Today 7.35am
the query i used, pretty much identical to what you supplied:
SELECT t.pub_datetime
, t.`date`
, t.score
FROM ( SELECT `date`
, MAX(pub_datetime) AS last_pub
FROM daTable
GROUP
BY `date` ) AS m
INNER
JOIN daTable AS t
ON t.`date` = m.`date`
AND t.pub_datetime = m.last_pub
so the problem is (due to me not giving all info i’m sure, sorry) there’s only one date per pub_datetime, apart from the last one, the ‘Today 7.35am’ one. how the ‘Today 7.35am’ one is working is how all the others should work. how to do that? thanks v. much 