Connecting Tech Pros Worldwide
 
 
sign in | join about | help | sitemap
[ http://bytes.com/adLoader.php?parent=database
djpaul's Avatar

Check if a record exists in the last 30 records


Question posted by: djpaul (Member) on October 5th, 2008 03:06 AM
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
post reply
Quote | Subscribe
7 Answers Posted
Atli's Avatar
Moderator - 2,750 Posts
#2: 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.
djpaul's Avatar
Member - 66 Posts
#3: 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
labmonkey111's Avatar
Newbie - 31 Posts
#4: 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
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.
djpaul's Avatar
Member - 66 Posts
#5: 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!
djpaul's Avatar
Member - 66 Posts
#6: 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 - 2,750 Posts
#7: 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
SELECT COUNT(`ID`) FROM `myTbl` WHERE `songID` = 1 AND `ID` > (     SELECT `ID` FROM `myTbl`     ORDER BY `ID` DESC     LIMIT 30, 1 )

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
djpaul's Avatar
Member - 66 Posts
#8: 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
Not the answer you were looking for? Post your question . . .
196,220 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,220 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top MySQL Contributors

[ http://bytes.com/adLoader.php?parent=database


You are viewing a mobilized version of this site...
View original page here

How do you rate mobile version of this page?

Mobilized by Mowser Mowser