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:
  #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: 25
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
Contributing User
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Colombo,Sri Lanka
Posts: 2,330 CodeKadiya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 50 m 36 sec
Reputation Power: 5
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: 25
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: 3
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: 3
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
Contributing User
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Colombo,Sri Lanka
Posts: 2,330 CodeKadiya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 50 m 36 sec
Reputation Power: 5
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
Contributing User
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Colombo,Sri Lanka
Posts: 2,330 CodeKadiya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 50 m 36 sec
Reputation Power: 5
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




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

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




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek