MySQL query to limit and sort
Anonymous Monk
created: 2006-02-03 14:32:20
I was wondering if it was possible to sort a mysql query for the last 10 rows (by id, desc I'd assume) and sort them desc by the column Total.

Is it possible to do this in one select statement? Or how would I go about doing this? I'd have to do two ORDER BYs.

Re: MySQL query to limit and sort
created: 2006-02-03 14:38:04
Do you mean something like this?
select id, Total
from (
    select id, Total
    from bar
    order by id desc
    limit 10
) baz
order by Total desc;
I think doing a simple select id, Total from bar order by id desc, Total desc limit 10; won't work since sorting by id is already exhaustive.
Re^2: MySQL query to limit and sort
created: 2006-02-03 14:52:24
Hi. Thanks for your code. One quick question.

I have like 5 columns total, including id and Total. Would I select these on the first line, inside the from() or both?

Sorry, that confuses me a little bit. Oh, and what is baz on the 2nd to the last line?

Re^3: MySQL query to limit and sort
created: 2006-02-03 15:13:03
I think you could get away with selecting the specific columns in the first line, while doing a generic "select *" in the inner select.

The baz is a table alias for the inner select, which I believe mysql requires (although I haven't verified if you can omit it). It allows you to explicitly name the columns in the outer select, e.g.

    select baz.id, foo.name
    from foo,                    -- regular table
    ( 
       select id, frob from bar
    ) baz                        -- a sub-select

    where foo.id = baz.frob      -- something that joins the two 
You can see there that it helps to disambiguate between foo.id and baz.id, that's why it's good that sub-selects can be named.
Re^4: MySQL query to limit and sort
created: 2006-02-04 00:24:53
Hi.

Sorry to bug you, you've been much help already. I am confused by this particular code.

   select baz.id, foo.name
    from foo,                    -- regular table
    ( 
       select id, frob from bar
    ) baz                        -- a sub-select

    where foo.id = baz.frob      -- something that joins the two 
I know it's pseudo code but I was wondering if perhaps you could show me a small working sample with columns: id, url, images and Total? I am pretty confused by what you mean here. The table is "temp".

Thank you.

Re^5: (OT) MySQL query to limit and sort
created: 2006-02-05 20:31:02
Here's your original request based on the column and table names you gave us:
select id, url, images, Total
from (
    select *
    from temp
    order by id desc
    limit 10
) last10
order by Total desc;
As for my pseudo-code you're confused about, that's only relevant if you have two tables that you might want to cross reference. As a more realistic example, suppose you have a database with Customers and Orders, and you want to find out your top ten buyers. You could do that like this:
  select Customers.name, sales.amount                     -- select name and total amount spent. 
  from Customers, 
    (                                                     -- a sub-select to find the 10 best buyers.
       select customer_id, sum(order_amount) as amount    -- This selects a column and an aggregate
       from Orders                                        -- from the real table Orders.
       group by customer_id
       order by amount desc
       limit 10
  ) sales                                                 -- we name this sub-select "sales".
  where sales.customer_id = Customers.id                  -- we INNER JOIN the two "tables", 
                                                          -- so we can cross-reference Orders against Customers.

As you can see, the "sales" sub-select isn't a real table in the database, but within this query we can treat it as if it were. We can select columns from it, and put extra constraints on it in a where clause. I hope it's a bit clearer now :)

Re: MySQL query to limit and sort
created: 2006-02-03 15:54:57
SELECT id, total
FROM table
ORDER BY id, total DESC
LIMIT 10;

That should do the trick. It orders by id (ascending which is the default), and then orders by total in descending order.

If you're using MySQL see their manual.
If you're using PostgreSQL see their manual.
Re^2: MySQL query to limit and sort
created: 2006-02-03 16:32:57
I tried your code but it only used the first 10 inserted rows. I had to change it to ORDER BY id DESC. Now it shows the newest 10 but it doesn't sort them by the total at all. Any idea why?
Re^3: MySQL query to limit and sort
created: 2006-02-03 16:39:58
As I already commented in Re: MySQL query to limit and sort, the sort on id is already exhaustive, meaning that sorting on total doesn't change anything. That is, every id value is different, so Total is never considered as a sorting criterion.

And that's exactly why I offered the sub-select as a solution: first grab the 10 most recent id's, then sort those on Total. You have to do the ordering in two steps, no way around that.

Re: MySQL query to limit and sort
created: 2006-02-03 17:12:24
  SELECT id, total
    FROM some_table
   WHERE id > (SELECT MAX(id) - 10)
ORDER BY total DESC
Re^2: MySQL query to limit and sort
created: 2006-02-03 17:40:12
Using that I don't get any rows back.

 my $data = qq[SELECT id, url, images, Total FROM temp WHERE id > (SELECT MAX(id) - 10) ORDER BY Total DESC];
Re^3: MySQL query to limit and sort
created: 2006-02-03 17:47:45
Apologies:
  SELECT id, total
    FROM some_table
   WHERE id > (SELECT MAX(id) FROM some_table) - 10
ORDER BY total DESC
Re^4: MySQL query to limit and sort
created: 2006-02-03 18:01:10
It's slightly fragile in the face of deletions. Say you have records 140 through 160, delete 150 through 159, then run this query. That will only return record 160, while I would expect (141 .. 149, 160). But that may just be my interpretation of the OP's question asking for a top 10.

perlmonks.org content © perlmonks.org and Anonymous Monk, rhesa, saberworks, shiza

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03