Google Answers Logo
View Question
 
Q: MySQL: Selecting consecutive rows ( No Answer,   0 Comments )
Question  
Subject: MySQL: Selecting consecutive rows
Category: Computers > Programming
Asked by: dedavai-ga
List Price: $20.00
Posted: 21 Jul 2005 14:12 PDT
Expires: 21 Jul 2005 14:58 PDT
Question ID: 546357
I have some experience with MySQL, but I'm bogged down with this
problem. How do I select an arbitrary number of consecutive rows from
the current row?

Here's an example. Suppose I have a table with 2 columns and 8 rows like this:

------------
id | value
------------
 1 | abc
 2 | xmen
 8 | irs
14 | fbi
17 | cia
22 | pda
40 | pdf
55 | six

Now, suppose I want to traverse the ID column, and select the VALUES
for the current ID and the next 4 IDs as well and CONCAT the values.
So the result would look like this:

------------
results
------------
abcxmenirsfbi /* id = 1 */
xmenirsfbicia /* id = 2 */
irsfbiciapda  /* id = 8 */
fbiciapdapdf  /* id = 14 */
ciapdapdfsix  /* id = 17, last ID with 4 values concatenated */
pdapdfsix     /* id = 22 */
pdfsix        /* id = 40 */
six           /* id = 55 */

I know I can load the results into an array in PHP and do it from
there, but my table has over 100,000 values and that defeats the point
of having a database.

Is there any way to do this?

Clarification of Question by dedavai-ga on 21 Jul 2005 14:57 PDT
Actually found the answer already:

SELECT CONCAT(X.value,Y.value) AS new_value FROM `myTable` AS x JOIN
`myTable` AS y ON x.id=y.id OR x.id=y.id+1 OR x.id=y.id+2  OR
x.id=y.id+3 OR x.id=y.id+4
Answer  
There is no answer at this time.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy