Jump to content

SELECT not in


Matrixkid

Recommended Posts

Hi there,

 

I need help constructing a select statement that in short selects songs from a database that have not been stored in any prior weeks or years.

 

Below is the table:

artist   |    song  |  weeknum  |    yearnum
lg            ab           49       2009
jvc           cd           49       2009
sony          ef           49       2009
lg            ab           31       2009
song          ef           22       2008

 

Lets assume the current week is 49.

From the above example, my desired result would be jvc.

Im trying to select all songs that were stored in week 49 of this year and never before. So, anything less than week 49, or any year less than 2009.

 

I had this statement working before but because of the amount of data it no longer works.

 

SELECT Count( song ) AS CountSong, artist , song FROM TESTDB 
WHERE weeknum =49 AND yearnum = 2009 AND
song NOT IN (SELECT song FROM TESTDB WHERE weeknum < 49 AND yearnum <= 2009) 
GROUP BY song ORDER BY `CountSong` DESC

 

I welcome all advice and opinions.

I am aware that the database structure isnt the greatest. Anything to make this sort of select statement work and i'll listen.

 

Thanks

MK

Link to comment
Share on other sites

you already have ? just remove the song not In ....

 

I think you misunderstood what results im looking for.

If i remove the not in part I am selecting all songs from week 49, in year 2009.

I want songs only that appeared in week 49, year 2009 and NEVER before.

 

thanks

MK

Link to comment
Share on other sites

Im trying the NOT EXISTS but keep getting 0 results.

 

SELECT Count(song) AS CountSong, artist , song FROM TESTDB
WHERE weeknum = 49 AND yearnum = 2009 AND
NOT EXISTS (SELECT artist,song FROM TESTDB WHERE weeknum < 49 AND yearnum <= 2009 GROUP BY song)
GROUP BY song ORDER BY `CountSong` DESC

Link to comment
Share on other sites

Affected rows: 0
Time: 0.032ms

 

Nope. Should return 1 row, JVC (from the table in the first post).

 

If I change NOT EXISTS to EXISTS, it gives me the opposite results. All the songs played prior to week 49. But as soon as I make it NOT then it returns nothing.

Link to comment
Share on other sites

thanks for the help Zanus, I appreciate it.

 

Unfortunately the revision doesnt work either. I have to remove the ) as songCheck part as it returns an error.

 

This statement returns no results:

SELECT * FROM TESTDB WHERE song NOT IN (
     SELECT DISTINCT song FROM TESTDB WHERE weeknum <= 49 AND yearnum <= 2009
)

 

This statement returns the correct result:

SELECT * FROM TESTDB WHERE song NOT IN (
     SELECT DISTINCT song FROM TESTDB WHERE weeknum < 49 AND yearnum <= 2009
)

 

I can run the NOT IN on the TESTDB and get the results i need, but as soon as I apply the query to my active db the query lags and goes nowhere. The database has over 190,000 records and grows every 3 hours.

 

heres the error: [Err] 1053 - Server shutdown in progress

 

Im not really sure where to go from here.

 

Thanks again.

MK

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.