Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
tricky sql: order by and limit
Old 12-08-2009, 12:26 AM tricky sql: order by and limit
Novice Talker

Posts: 9
Name: Yoke
Trades: 0
Hi

I'm creating a Magento-like sorting function on my product table (on back-end of my website).
Somehow I'm getting trouble to apply sort function (with where clause) only to the second 20 records of the table.
What it does is sort the whole table first then cut the result by limit clause.
What I need to do is get the 20 certain records from a table then sort it out.

Is there any other workaround for this one?

Thanks in advance.
yoke.lee is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-08-2009, 01:55 AM Re: tricky sql: order by and limit
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Maybe it will be obvious to someone else without more information, but if I had the query, it'd be a bit easier.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 12-08-2009, 01:58 AM Re: tricky sql: order by and limit
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Use an inner query...
Code:
select * 
from (
  select *
  from yourTable
  order by someField
)
limit 20
Edit:
The principle is valid, but I switched the conditions by error.
See this post: http://www.webmaster-talk.com/the-da...tml#post958637
to the correct one.

This will be executed from the inside toward the outside.
First, the inner query with the order by will be launched, and then, you will just pick the 20 first fields of that query with the outer part.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 12-08-2009 at 04:25 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 12-08-2009, 01:59 AM Re: tricky sql: order by and limit
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
And... there came the "someone else"
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 12-08-2009, 02:02 AM Re: tricky sql: order by and limit
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0

What can I say...
DB is my trade.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 12-08-2009, 03:23 AM Re: tricky sql: order by and limit
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
And just to add:

If your DB server is MySql it will need to be ver 4.1 or above to support the subquery syntax.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-08-2009, 04:25 AM Re: tricky sql: order by and limit
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Hum...
Proof-reading the OP post, I realize that I switched the conditions...

The query should be:
Code:
select *
from (
  select *
  from yourTable
  order by aField desc
  limit 20
)
order by someOtherField
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 12-15-2009, 02:17 AM Re: tricky sql: order by and limit
Novice Talker

Posts: 9
Name: Yoke
Trades: 0
yea, tripy, that one works. ^ ^
thanks alot. you're the guy!
yoke.lee is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to tricky sql: order by and limit
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.92330 seconds with 12 queries