So m.zam worked out the answer. Thank you very much. Can anyone take this a little bit further? Here's the two tables. There are two things I'm trying to do now, replace a returned null date with the words 'New Song' and I'm trying to find out how many shows have there been since a song was last played.
song_id song_name
51 | Song Name A
368 | Song Name B
168 | Song Name C
568 | Song Name D
13 | Song Name E
song_id | song_order| show_date |show_id
368 | 1 | 2010-02-17 00:00:00 | 367
368 | 0 | 2012-04-06 00:00:00 | 499
51 | 2 | 2012-01-19 00:00:00 | 399
51 | 2 | 2013-04-24 00:00:00 | 870
51 | 8 | 2013-07-19 00:00:00 | 899
368 | 2 | 2013-07-19 00:00:00 | 899
13 | 5 | 2013-07-19 00:00:00 | 899
568 | 2 | 2013-07-19 00:00:00 | 899
368 | 4 | 2012-06-08 00:00:00 | 799
168 | 2 | 2013-06-28 00:00:00 | 896
568 | 2 | 2013-07-03 00:00:00 | 897
568 | 2 | 2010-02-21 00:00:00 | 897
My code below is working to return this but I need some more data. I'm looking to turn the first table into the second. Thanks in advanced! Code can be found here: http://sqlfiddle.com/#!2/f3e29/1
FIRST
SHOW_DATE | SHOW_ID | SONG_NAME | SONG_ID | PREVDATE |
July, 19 2013 | 899 | Song Name A | 51 | April, 24 2013 |
July, 19 2013 | 899 | Song Name B | 368 | June, 08 2012 |
July, 19 2013 | 899 | Song Name E | 13 | (null) |
July, 19 2013 | 899 | Song Name D | 568 | July, 03 2013 |
SECOND
SHOW_DATE | SHOW_ID | SONG_NAME | SONG_ID | PREVDATE | SHOWS SINCE
July, 19 2013 | 899 | Song Name A | 51 | April, 24 2013 | 2
July, 19 2013 | 899 | Song Name B | 368 | June, 08 2012 | 4
July, 19 2013 | 899 | Song Name E | 13 | NEW SONG | 0
July, 19 2013 | 899 | Song Name D | 568 | July, 03 2013 | 0
SELECT a.show_date, a.show_id,
b.song_name, a.song_id, (
SELECT
IFNULL(MAX(show_date),'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
Though IFNULL works in SQL, im using PHP to change the format of the returned date to something more like mm/dd/yy, could I change the date in SQL before sending it to PHP or write a IF ELSE statement in the PHP to return either mm/dd/yy or 'NEW SONG'
echo "<td style='padding: 10px; width:45px;'>" .date("m/d/y",
strtotime($row["PrevDate"]))." </td>";
Try this for the first part (NEW SONG)
SELECT a.show_date, a.show_id,
b.song_name, a.song_id,
(
SELECT IFNULL(MAX(show_date),'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 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.