If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > select rows where id in (select...limit x) limit z Not Allowed?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-18-07, 23:41
djuritz djuritz is offline
Registered User
 
Join Date: Sep 2005
Posts: 67
select rows where id in (select...limit x) limit z Not Allowed?

Hi.

I have a table with videos (id, title, views)
I want to display the most viewed videos.
I don't want to show always the same top ten por example.
The solution is to select the top 30 and then get 5 random from them.

MySQL version is 5.0.45 (lastest)

The query is:
SELECT * FROM video WHERE id IN (
SELECT id
FROM video v
ORDER BY visitas
LIMIT 30) x
ORDER BY rand()
LIMIT 10

(alias X and V are mandatory for MySQL)

The problem is I get this error: #1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

Is there any solution?
The only way is by using 2 queries and PHP. Isn't it?
The first one to get top 30 (just id's) and then top 10 where id IN (idīs from first query).

Thanks a lot!
Reply With Quote
  #2 (permalink)  
Old 10-19-07, 01:40
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
my advice: return the top 30, then use php to pick 10 at random

keeps the query simple

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On