Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old October 28th, 2003, 10:43 AM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
mysql select question

Hi!

I am wondering how I can find out the index of a row in a MySql table given a certain order. For example I have a table containing information of some images on my computer. After

SELECT * FROM Images WHERE Id='12' ORDER BY Date

I get an Image and want to find the next image (next in the Date order). So I would like something as

index = GETINDEX FROM Images WHERE Id='12' ORDER BY Date
nextimage = SELECT * FROM Images WHERE ORDER BY Date LIMIT index+1, index+1

Someone know how to code this into a proper SQL query?

thanks,
pieter

Reply With Quote
  #2  
Old October 28th, 2003, 11:31 AM
CodeKadiya CodeKadiya is offline
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Colombo,Sri Lanka
Posts: 2,313 CodeKadiya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
Send a message via Yahoo to CodeKadiya
RE: mysql select question

$imageNo=4; // Image number you want to view

$from=$imageNo-1;
$sql="SELECT * FROM images ORDER BY Date LIMIT $from,$imageNo";

Reply With Quote
  #3  
Old October 29th, 2003, 06:37 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: mysql select question

Yes, this is the way to get a certain row with a certain index. But how do I find out the correct value of $imageNo?

The images have a unique Id, but the order of the Id's is not the order by Date. So given a (unique) Id number, how do I efficiently find out the index of this image if ordered by Date?

thanks
,pieter

Reply With Quote
  #4  
Old October 29th, 2003, 07:15 PM
-vertigo- -vertigo- is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Louth, Lincolnshire
Posts: 314 -vertigo- User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 24 sec
Reputation Power: 2
RE: mysql select question

If you want to get a specific image, use the primary key of the table.

If you want to get a number of images ordered by date, select and order by date.

If you want the fourth image, or 5 images starting from the fourth, use limit like CodeKadiya said.

Don't mix these things up. It sounds like you want to work with one image, then get the next one. In this case it is better to select all the images you will use in one query, then simply use them.

Don't use to many queries, or too fancy queries, use the ones that work the best/easiest.

Especially with database programming, try to make the code as easy to understand as possible, because often you want to change something, and it is easier if the code is straightforward.


Reply With Quote
  #5  
Old October 29th, 2003, 07:48 PM
-vertigo- -vertigo- is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Louth, Lincolnshire
Posts: 314 -vertigo- User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 24 sec
Reputation Power: 2
RE: mysql select question

As a more specific answer, (select date from table where index = '12'), then select where date is after that date, and use limit.

You might need to RTFM to see how to compare dates. You must look in the manual to see what you need to do.

You can also place an index on the date column, that way you can select images by the date easily.

Reply With Quote
  #6  
Old October 30th, 2003, 12:09 AM
CodeKadiya CodeKadiya is offline
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Colombo,Sri Lanka
Posts: 2,313 CodeKadiya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
Send a message via Yahoo to CodeKadiya
RE: RE: mysql select question


Quote:
Yes, this is the way to get a certain row with a certain index. But how do I find out the correct value of $imageNo?


What do you mean by 'find out'? You want to display it or what?? You can change the value of $imageNo dynamically and get the picture in that index. Just echo $imageNo to see what image it is....

Reply With Quote
  #7  
Old October 30th, 2003, 12:13 AM
CodeKadiya CodeKadiya is offline
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Colombo,Sri Lanka
Posts: 2,313 CodeKadiya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
Send a message via Yahoo to CodeKadiya
RE: RE: mysql select question


Quote:
If you want the fourth image, or 5 images starting from the fourth, use limit like CodeKadiya said.


Yes... he can just use this to get if he want to get 7th image from the table...
$sql="SELECT * FROM images ORDER BY Date LIMIT 7,7";

And he can change the 7 dynamically by assinging into a variable and changing it's value.

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > mysql select question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway