TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-24-2007, 11:05 AM   #1 (permalink)
The Contributor
Upcoming Programmer 
 
meshi's Avatar
 
Join Date: Oct 2007
Posts: 44
Thanks: 0
meshi is on a distinguished road
Default 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 thisponml
i want my display would be edcba
meshi is offline  
Reply With Quote
Old 10-24-2007, 11:39 AM   #2 (permalink)
The Contributor
Upcoming Programmer 
 
Gurnk's Avatar
 
Join Date: Oct 2007
Location: US
Posts: 66
Thanks: 19
Gurnk is on a distinguished road
Default

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"
Send a message via MSN to Gurnk
Gurnk is offline  
Reply With Quote
Old 10-24-2007, 11:55 AM   #3 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

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.

Last edited by bluesaga : 10-24-2007 at 11:57 AM. Reason: [code] looks horrible, [php] is much nicer
bluesaga is offline  
Reply With Quote
Old 10-24-2007, 04:49 PM   #4 (permalink)
The Contributor
Upcoming Programmer 
 
meshi's Avatar
 
Join Date: Oct 2007
Posts: 44
Thanks: 0
meshi is on a distinguished road
Default

yeah it works...thanks bluesaga
meshi is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

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


All times are GMT. The time now is 05:34 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design