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.

PHP Forum


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



Freelance Jobs

Reply
MySQL - Can I LIMIT before ORDER BY?
Old 05-26-2009, 06:10 AM MySQL - Can I LIMIT before ORDER BY?
EdB
Skilled Talker

Posts: 79
Name: Ed Barnett
Trades: 0
Hi all.

I'm trying to get results from a table BUT am running into an issue. Normally a query would run as follows;

.... ORDER BY blah blah LIMIT x, y

1. Order the results using ORDER BY
2. Limit the results starting from 'x' for 'y' results

My issue is that I want to do the two in reverse order;


.... LIMIT x, y ORDER BY blah blah

1. Limit the results starting from 'x' for 'y' results
2. Order the results using ORDER BY

Is there any way to do this? I know that simply restructuring the query doesn't work. Any help would be greatly appreciated

Thanks guys
__________________

Please login or register to view this content. Registration is FREE
EdB is offline
Reply With Quote
View Public Profile Visit EdB's homepage!
 
 
Register now for full access!
Old 05-26-2009, 07:51 AM Re: MySQL - Can I LIMIT before ORDER BY?
Super Talker

Posts: 134
Trades: 0
what do you want achieve with this ?
__________________

Please login or register to view this content. Registration is FREE

Check out the Facebook Clone build with Jcow SNS at
Please login or register to view this content. Registration is FREE
, it is free and it always will be
Falcone is offline
Reply With Quote
View Public Profile
 
Old 05-26-2009, 07:56 AM Re: MySQL - Can I LIMIT before ORDER BY?
EdB
Skilled Talker

Posts: 79
Name: Ed Barnett
Trades: 0
Hi Falcone.

I have a page that allows paging of results (it currently shows customers their invoices). I also want to allow users to click on the table headings (e.g. invoice number, purchase order number, total etc.) to order that page's invoices accordingly.

The problem is that if I'm on page 2 which displays invoices 1005 -1009 and the user clicks on the heading to arrange them by total, it doesn't just sort the invoices on that page. It will order ALL of their invoices by total and return invoices from the 5th to 9th position.

Bit of a difficult one to explain, does that make sense?
__________________

Please login or register to view this content. Registration is FREE
EdB is offline
Reply With Quote
View Public Profile Visit EdB's homepage!
 
Old 05-26-2009, 08:17 AM Re: MySQL - Can I LIMIT before ORDER BY?
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Why would sorting the results on one page affect another? Before we go further, show us some of the code you are using.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 05-26-2009, 08:32 AM Re: MySQL - Can I LIMIT before ORDER BY?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
As the other said, first be sure that it's really what you want.
The only practical use I had of this is when I wanted to randomize the last x results of a list.

To achieve this, you need to use a subquery to get the x records you want, and then you order them, like this:
Code:
select *
from (
  select x,y,z
  from tblSource
  order by z desc
  limit 10 offset 0
) as tbl1
order by tbl1.x asc
__________________
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!
 
Reply     « Reply to MySQL - Can I LIMIT before ORDER BY?
 

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.21016 seconds with 12 queries