WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Database Discussion Forum This is the place to find help resolving those nagging questions you have about implementing and using all kinds of databases. Need help writing a query? Need an opinion on Oracle? Post here!

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-24-2007, 11:00 AM
WebProWorld Pro
 

Join Date: Aug 2007
Location: Southern Illinois USA
Posts: 146
kruser RepRank 0
Default MYSQL - ORDER BY x DESC

Trying to get the last 4 records from a table that have a distinct var1. The following returns the first 4 records in descending order. Any thoughts would be appreciated.

mysql_query("SELECT distinct var1, var2, var3 FROM tablename Where ((var2<1200) | (var2>1299)) ORDER BY var3 DESC LIMIT 4" , $linkID) or die("connection not made");

Best Regards,
Randy
Illinois Real Estate Mobi
Reply With Quote
  #2 (permalink)  
Old 08-27-2007, 12:02 PM
jganders's Avatar
WebProWorld Member
 

Join Date: Jul 2007
Location: California, USA
Posts: 26
jganders RepRank 0
Default Re: MYSQL - ORDER BY x DESC

too vague. btw, who names their db cols var1, var2, var3??? r ur real col names top secret? are you trying to get the last 4 records in logical order? or in var1 order? then why order by var3?? provide some data or sumfin.
Reply With Quote
  #3 (permalink)  
Old 08-27-2007, 01:47 PM
wige's Avatar
Moderator
WebProWorld Moderator
 

Join Date: Jun 2006
Location: United States
Posts: 1,722
wige RepRank 4wige RepRank 4wige RepRank 4wige RepRank 4
Default Re: MYSQL - ORDER BY x DESC

I take it you mean the last four based on the column var3?
Stripping down to the most basic version of the query:
SELECT var1, var2 FROM table WHERE (var2 < 1200) ORDER BY var3 DESC LIMIT 4
I swear, I looked at this for about twenty minutes, dug through all the documentation I could find on the MySQL site, did a bunch of Google searches, before saying... duhhh!
SELECT var1, var2 FROM table WHERE (var2 < 1200) ORDER BY var3 ASC LIMIT 4

Limit always starts with the first result, so if you want the last four results, you simply reverse the sort order.
__________________
The best way to learn anything, is to question everything.
Interestingly Average Security Blog

Last edited by wige : 08-27-2007 at 01:50 PM.
Reply With Quote
  #4 (permalink)  
Old 08-27-2007, 08:56 PM
jganders's Avatar
WebProWorld Member
 

Join Date: Jul 2007
Location: California, USA
Posts: 26
jganders RepRank 0
Default Re: MYSQL - ORDER BY x DESC

but his cryptic question said (i think) he said the table has a "distinct var1". so i'm thinkin he wants var1 in desc ord. he's gotta weigh in on this. maybe u guessed right wige.
Reply With Quote
  #5 (permalink)  
Old 08-28-2007, 10:45 AM
wige's Avatar
Moderator
WebProWorld Moderator
 

Join Date: Jun 2006
Location: United States
Posts: 1,722
wige RepRank 4wige RepRank 4wige RepRank 4wige RepRank 4
Default Re: MYSQL - ORDER BY x DESC

From his description, I get the feeling that the query he posted was returning the four records in the correct order, but from the wrong end of the data set. By changing it to my post (from DESC to ASC) he will get the correct pieces of data, but in the reverse order. Most likely, the data is being put into an array in his program, so he would simply need to display or process those array items in reverse order so that he will now have the correct data pieces in the correct order.

for ($i=length($result) -1; $i >= 0; $i--) instead of for ($i= 0; $i < length($result); $i++)
__________________
The best way to learn anything, is to question everything.
Interestingly Average Security Blog
Reply With Quote
  #6 (permalink)  
Old 08-28-2007, 12:03 PM
WebProWorld Pro
 

Join Date: Aug 2007
Location: Southern Illinois USA
Posts: 146
kruser RepRank 0
Default Re: MYSQL - ORDER BY x DESC

Thanks for all the great input.

The original script worked on one server, but differently on another. I stripped it down a bit and is now working great on both servers! The script is for a real estate site. On the home page it provides the four most recently posted listings, but not duplicate towns so... distict to the rescue

New script - mysql_query("SELECT distinct town_name FROM real_estate_listings ORDER BY table_id DESC LIMIT 4" , $linkID) or die("connection not made b");

When I was first learning mysql and php it was helpful when people used examples calling variables "var1" "var2" "table_name" etc. so when a newbie reads this post, i thought it would be easier.

Best Regards,
Randy
Reply With Quote
  #7 (permalink)  
Old 08-30-2007, 06:22 AM
WebProWorld New Member
 

Join Date: Aug 2007
Posts: 6
textypisni32 RepRank 0
Default Re: MYSQL - ORDER BY x DESC

Should be ORDER BY var3,var2 DESC LIMIT 4

hope it works
_________________

Last edited by mjtaylor : 09-04-2007 at 11:07 PM. Reason: excessive links
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum
Tags: , ,



Thread Tools
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Title and Desc Tags - BAD??????? schmeetz Search Engine Optimization Forum 3 11-09-2006 12:04 PM
Wierdest thing....my Title and Desc slaughtered in SERP's schmeetz Marketing Strategies Discussion Forum 2 04-20-2006 02:59 PM
Order.com JKomp Domain Discussion Forum 5 04-05-2005 01:11 PM
Was 4th in Google, now updated & has no Desc or Title... MichaelOffe Search Engine Optimization Forum 6 02-21-2005 08:42 AM
Google dont show my title and desc. of indexed pages Ankushm Google Discussion Forum 4 02-14-2005 09:09 AM


Search Engine Optimization by vBSEO 3.2.0