TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   how to sort this???? (http://www.talkphp.com/absolute-beginners/1333-how-sort.html)

meshi 10-24-2007 11:05 AM

how to sort this????
 
$sql="select * from my table order by lastname LIMIT 0,5"

if i have this display
lastname
a
b
c
d
e

and i have a data in my table
a b c d e f g h i j k l m n o p

i want to sort only the abcde by desc.what would i do?

what i did is this one
$sql="select * from my table order by lastname sort desc LIMIT 0,5"
this query displays this--->ponml
i want my display would be edcba

Gurnk 10-24-2007 11:39 AM

PHP Code:

$sql "SELECT * FROM table ORDER by lastname ASC"

That should do it "abcdef" or changing it to DESC will do "fedcba"

Limit it to 5 would be
PHP Code:

$sql "SELECT * FROM table ORDER by lastname ASC LIMIT 0,5"


bluesaga 10-24-2007 11:55 AM

Gurnk, i don't think you understand him.

On such a small dataset it wouldn't be very good to use mysql the way i propose and use PHP instead, however with a larger dataset i see why you would be concerned.

The method i recommend to fix this would be to use a sub-query to fetch the ID's to rank for like so:

PHP Code:

SELECT
    t
.*
FROM
    
(SELECT FROM table ORDER BY lastname ASC LIMIT 0,5t
ORDER BY
    t
.lastname DESC 

Note: This is not tested and may not work, but it should do :)

What this does, is create a result set to work with first (temporary table) and then do the ordering after.

meshi 10-24-2007 04:49 PM

yeah it works...thanks bluesaga


All times are GMT. The time now is 02:49 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0