Connecting Tech Pros Worldwide Forums | Help | Site Map

Check if a record exists in the last 30 records

Member
 
Join Date: Oct 2006
Location: Netherlands
Posts: 92
#1: Oct 5 '08
Hello!
I want to check if a record exists in the last 30 records that were added.
For example, i have a table wich contains about 34000 records by now.
But i want to check if the songID is in the last 30 records.

Is this somehow possible?

Now i select the last 30 records and put them in an array, and let php check if this songID is in the array.
But it must be much easier if mysql this does!

Regards!
Paul

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,752
#2: Oct 5 '08

re: Check if a record exists in the last 30 records


Hey Paul.

How does your table look like?
Is the "songID" column the primary key for that table?

I would be very surprised if this isn't possible, but without knowing exactly how you table looks like I can't really say for sure.
Member
 
Join Date: Oct 2006
Location: Netherlands
Posts: 92
#3: Oct 5 '08

re: Check if a record exists in the last 30 records


No, it's like this:

ID - int - primary key - auto increment
songID int -
filename varchar
date_played - datetime
artist varchar
title varchar
...and furter
Member
 
Join Date: Sep 2008
Posts: 40
#4: Oct 5 '08

re: Check if a record exists in the last 30 records


This may not be the ideal way to do it, but this might work:

Expand|Select|Wrap|Line Numbers
  1. SELECT songID FROM songs WHERE songID IN (SELECT songID FROM songs ORDER BY ID DESC LIMIT 0, 30);
Then check mysql_num_rows to see if anything was returned.
Member
 
Join Date: Oct 2006
Location: Netherlands
Posts: 92
#5: Oct 6 '08

re: Check if a record exists in the last 30 records


Hmm, that may work.
I gonna try it tomorrow and let you know!

Thanks!
Member
 
Join Date: Oct 2006
Location: Netherlands
Posts: 92
#6: Oct 7 '08

re: Check if a record exists in the last 30 records


Hmm, too bad!
I get an error: #1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

I just updated mysql from 5.0.21 to 5.1.28 but it didn't seem to help.
Strange...
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,752
#7: Oct 7 '08

re: Check if a record exists in the last 30 records


Strange. I thought that worked in all versions 5 and above...

Anyhow. You could also try something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(`ID`) FROM `myTbl`
  2. WHERE `songID` = 1
  3. AND `ID` > (
  4.     SELECT `ID` FROM `myTbl`
  5.     ORDER BY `ID` DESC
  6.     LIMIT 30, 1
  7. )
  8.  
The idea is to fetch the ID of the 30'th row from the top using the subquery and match only against rows with higher ID's.

Tested this on version 5.0.51
Member
 
Join Date: Oct 2006
Location: Netherlands
Posts: 92
#8: Oct 8 '08

re: Check if a record exists in the last 30 records


Aahh!
That's a lot better.
It maybe a bug of mysql, but i think it's strange that you cannot use a limit in a IN function.
Too bad!

Thanks Atli!

Paul
Reply


Similar MySQL Database bytes