OsakaWebbie
Programmer
Thanks to r937's tireless help on two occasions, I have two pieces of amazing code that appear to work well on their own. For reference, the two related threads are and But when I need to combine them in the same query, records start disappearing again. Pardon this long first post, but I feel the need to provide four complete queries straight from my code, for clarity. Note: all unqualified fields are from the pw_song table. Also note that in some cases you may see what looks like silly extra code (like "WHERE 1", GROUP BYs with no purpose, and SUMs of CASES with only one case), but these queries are assembled by PHP code based on a multitude of possible criteria, and this method keeps that code as simple as possible. As far as I can tell, the extra SQL is harmless (maybe slightly slower, but this is not a large DB).
If I ask for all records, with no "filters" (see thread 1107522) and no request for the last usage date (see thread 1130521), it looks like this:
If I add the code for getting the last usage date, it looks like this:
...and I still get all the records in the database, as I ought.
If I use only the filter function, with no "last usage" code, it looks like this:
... and I get the correct number of records, all the ones that have a records for KeywordID=37. (The reason I use the "IN" syntax is that I can add other numbers to the list, and even another clause for exclusions. For this example I just had one for simplicity; there's no point in testing more complex cases until I get the simple one working).
But here's where the problem happens: when I try to combine the two functions together, I lose some records, a different number depending on which EventID I try to get the last date for. The query I attempted is this:
I'm concerned about how my joins are connecting - both the pw_songkey and usage1 joins work when directly after pw_song, but when I'm doing both of them, they end up kinda daisychained, if you know what I mean. But I don't know if that's a problem or not, or if it is, how to fix it.
If the "join wizard" r937 is listening, do you see how to combine the two ideas you gave me? Or does someone else see what I'm doing wrong and how to fix it?
If I ask for all records, with no "filters" (see thread 1107522) and no request for the last usage date (see thread 1130521), it looks like this:
Code:
SELECT pw_song.SongID, Title, Tagged, OrigTitle, Tempo, SongKey, LEFT(Lyrics,INSTR(Lyrics,'\n')-1) AS FirstLine FROM pw_song LEFT OUTER JOIN pw_songkey ON pw_song.SongID=pw_songkey.SongID WHERE 1 GROUP BY pw_song.SongID, Title, Tagged, OrigTitle, Tempo, SongKey, FirstLine ORDER BY OrigTitle
Code:
SELECT pw_song.SongID, Title, Tagged, OrigTitle, Tempo,SongKey, LEFT(Lyrics,INSTR(Lyrics,'\n')-1) AS FirstLine, usage1.UseDate AS Last FROM pw_song LEFT OUTER JOIN pw_songkey ON pw_song.SongID=pw_songkey.SongID LEFT OUTER JOIN pw_usage AS usage1 ON pw_song.SongID=usage1.SongID AND usage1.EventID=2 LEFT OUTER JOIN pw_usage as usage2 ON pw_song.SongID=usage2.SongID AND usage2.EventID=2 WHERE 1 GROUP BY pw_song.SongID, Title, Tagged, OrigTitle, Tempo, SongKey, FirstLine, Last HAVING Last=MAX(usage2.UseDate) OR Last IS NULL ORDER BY OrigTitle
If I use only the filter function, with no "last usage" code, it looks like this:
Code:
SELECT pw_song.SongID, Title, Tagged, OrigTitle, Tempo, SongKey, LEFT(Lyrics,INSTR(Lyrics,'\n')-1) AS FirstLine FROM pw_song LEFT OUTER JOIN pw_songkey ON pw_song.SongID=pw_songkey.SongID WHERE 1 GROUP BY pw_song.SongID, Title, Tagged, OrigTitle, Tempo, SongKey, FirstLine HAVING SUM(CASE WHEN pw_songkey.KeywordID IN (37) then 1 else 0 end) = 1 ORDER BY OrigTitle
But here's where the problem happens: when I try to combine the two functions together, I lose some records, a different number depending on which EventID I try to get the last date for. The query I attempted is this:
Code:
SELECT pw_song.SongID, Title, Tagged, OrigTitle, Tempo, SongKey, LEFT(Lyrics,INSTR(Lyrics,'\n')-1) AS FirstLine, usage1.UseDate AS Last FROM pw_song LEFT OUTER JOIN pw_songkey ON pw_song.SongID=pw_songkey.SongID LEFT OUTER JOIN pw_usage AS usage1 ON pw_song.SongID=usage1.SongID AND usage1.EventID=2 LEFT OUTER JOIN pw_usage as usage2 ON pw_song.SongID=usage2.SongID AND usage2.EventID=2 WHERE 1 GROUP BY pw_song.SongID, Title, Tagged, OrigTitle, Tempo, SongKey, FirstLine, Last HAVING SUM(CASE WHEN pw_songkey.KeywordID IN (37) then 1 else 0 end) = 1 AND (Last=MAX(usage2.UseDate) OR Last IS NULL) ORDER BY OrigTitle
If the "join wizard" r937 is listening, do you see how to combine the two ideas you gave me? Or does someone else see what I'm doing wrong and how to fix it?