MySQL Find previous date that a song was played

会有一股神秘感。 提交于 2020-01-07 02:02:46

问题


I'm looking for any help to why my code here isn't working anymore. SQL Fiddle links for the ORIGINAL and MODIFIED code. I had my database in two different tables and this code worked fine.

SELECT DATE_FORMAT(a.show_date,'%m/%d/%y') as show_date, a.song_order, a.show_id, 
 b.song_name, a.song_id,  (
 SELECT 
 IFNULL(MAX(DATE_FORMAT(show_date,'%m/%d/%y')), 'NEW SONG')
 FROM tbl_shows AS c
 WHERE a.show_date > c.show_date and a.song_id = c.song_id
 ) As PrevDate 
 FROM tbl_shows a, tbl_songs b
 WHERE a.song_id = b.song_id 
 AND a.show_id = 899

The tables were:

CREATE TABLE tbl_songs
(`song_id` int, `song_name` varchar(11))
;

INSERT INTO tbl_songs
(`song_id`, `song_name`)
VALUES
(51, 'Song Name A'),
(368, 'Song Name B'),
(168, 'Song Name C'),
(568, 'Song Name D'),
(13, 'Song Name E')
 ;

 CREATE TABLE tbl_shows
(`song_id` int, `song_order` int, `show_date` datetime, `show_id` int)
 ;

 INSERT INTO tbl_shows
(`song_id`, `song_order`, `show_date`, `show_id`)
VALUES
    (51, 1, '2013-07-19 00:00:00', 899),
    (568, 2, '2013-07-19 00:00:00', 899),
    (168, 3, '2013-07-19 00:00:00', 899),
    (13, 4, '2013-07-19 00:00:00', 899),
    (368, 1, '2013-07-06 00:00:00', 898),
    (368, 1, '2013-07-03 00:00:00', 897),
    (368, 1, '2013-05-27 00:00:00', 896),
    (51, 1, '2013-04-10 00:00:00', 895),
(168, 1, '2013-04-10 00:00:00', 895),
(513, 1, '2013-03-12 00:00:00', 894),
(13, 1, '2013-03-03 00:00:00', 893);

In order for my database to be more efficient and I didn't have to repeat the date over and over I separated it out into another table called tbl_song_shows... like this.

 CREATE TABLE tbl_songs
(`song_id` int, `song_name` varchar(11))
 ;

 INSERT INTO tbl_songs
(`song_id`, `song_name`)
 VALUES
(51, 'Song Name A'),
(368, 'Song Name B'),
(168, 'Song Name C'),
(568, 'Song Name D'),
(13, 'Song Name E')
;

CREATE TABLE tbl_shows
(`song_id` int, `song_order` int, `show_date` datetime, `show_id` int)
;

INSERT INTO tbl_shows
(`show_date`, `show_id`)
VALUES
( '2013-07-19 00:00:00', 899),
( '2013-07-19 00:00:00', 899),
( '2013-07-19 00:00:00', 899),
( '2013-07-19 00:00:00', 899),
( '2013-07-06 00:00:00', 898),
( '2013-07-03 00:00:00', 897),
( '2013-05-27 00:00:00', 896),
( '2013-04-10 00:00:00', 895),
( '2013-04-10 00:00:00', 895),
( '2013-03-12 00:00:00', 894),
( '2013-03-03 00:00:00', 893);

CREATE TABLE tbl_song_shows
(`song_id` int, `song_order` int, `show_id` int)
;

INSERT INTO tbl_song_shows
(`song_id`, `song_order`, `show_id`)
VALUES
 (51, 1, 899),
 (568, 2, 899),
 (168, 3, 899),
 (13, 4, 899),
 (368, 1, 898),
 (368, 1, 897),
 (368, 1, 896),
 (51, 1, 895),
 (168, 1, 895),
 (513, 1, 894),
 (13, 1, 893);

I changed my code to reflect the the additional table but it doesn't return the same query data. Please see the two attached SQL Fiddle links for the ORIGINAL and MODIFIED code. I'm looking for the PrevDate to return the previous date that a particular song was played. In the new code it returns the PrevDate but not based on song. Any help to get me back on track would be appreciated. Thanks. Bonus if afterwards someone could tell me how to then count the shows between that Show_Date and PrevDate. Thanks!!!

来源:https://stackoverflow.com/questions/18324730/mysql-find-previous-date-that-a-song-was-played

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!