简体   繁体   中英

SQL Finding the previous date from another row matching query

Here's a two tables which I have joined together...

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 

I've worked out what I'm trying to do on SQL Fiddle link but I can't get the code right. I'm trying based on a value of the show_id say its '899', return all the rows equal to 899, show_ids all have the same date assigned to them, but based on song id I want to return the previous date that the song_id comes up.

I'd like the data to return...

  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 | April, 08 2012 
July, 19 2013  |     899 | Song Name E |      13 | FIRST TIME 
July, 19 2013  |     899 | Song Name D |     568 | July, 03 2013

My code here is just returning the previous date before July, 19 2013, which is July, 03 2013 and putting that for every PrevDate. Could anyone help me? I'm not the most experienced coder and I believe I can't do what I did here because I'm grouping a subquery. Thanks in advanced!

 SELECT tbl_shows.show_date, tbl_shows.show_id, 
 tbl_songs.song_name, tbl_shows.song_id, (
    SELECT
        MAX(show_date)
    FROM tbl_shows AS fdate
    WHERE tbl_shows.show_date > show_date 
 ) As PrevDate
  FROM tbl_shows LEFT JOIN tbl_songs
  ON tbl_shows.song_id = tbl_songs.song_id
  WHERE tbl_shows.show_id = 899

Looks like you just need to tell MySQL to group the results by date:

SELECT tbl_shows.show_date, tbl_shows.show_id, 
tbl_songs.song_name, tbl_shows.song_id, (
   SELECT
       MAX(show_date)
   FROM tbl_shows AS fdate
   WHERE tbl_shows.show_date > show_date 
) As PrevDate
FROM tbl_shows LEFT JOIN tbl_songs
ON tbl_shows.song_id = tbl_songs.song_id
GROUP BY tbl_shows.show_date
HAVING tbl_shows.show_id = 899

EDIT: Or, as Dan Bracuk pointed out, you could do this:

SELECT tbl_shows.show_date, tbl_shows.show_id, 
tbl_songs.song_name, tbl_shows.song_id, (
   SELECT
       MAX(show_date)
   FROM tbl_shows AS fdate
   WHERE tbl_shows.show_date > show_date 
) As PrevDate
FROM tbl_shows
LEFT JOIN tbl_songs
ON tbl_shows.song_id = tbl_songs.song_id
WHERE tbl_shows.show_id = 899
GROUP BY tbl_shows.show_date
ORDER BY tbl_shows.show_id DESC
LIMIT 0, 10

Note: the last two lines are more of an example than anything else.

try this ...

  SELECT a.show_date, a.show_id, 
  b.song_name, a.song_id, (
  SELECT
    MAX(show_date)
  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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM