If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > problem with row numbers in query result

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-12-04, 02:59
jicco jicco is offline
Registered User
 
Join Date: Oct 2004
Posts: 1
php/mysql row numbers in query result

Hi guys,

Is it somehow possible to retrieve the position of a certain row in the results of a mysql query?

To be more expecific, I have a table containing products.
Now, this products belong to different product groups. The products are qualified by groups in a another table where I
use the product ID and the group ID.

Like this

Products
****************
id | name

1 product_a
2 product_b
3 product_c
4 product_d
5 product_e
6 product_f
7 product_g
8 product_h

Groups
****************
id | name

1 | group_1
2 | group_2


Products_grouped
****************
product_id | group_id

1 | 2
2 | 1
3 | 1
4 | 2
5 | 1
6 | 2
7 | 2
8 | 1



When I print some product I want to have navigation links
which will show the next or last product that belongs to the same group. Eg: if I print the product number 5, the "next" link will print the product number 8 and the "last" will print the product number 3. To achieve this I need to work with ID numbers. But how would be possible to get the last product and next product ID when they are not consecutive numbers? Im working with PHP.

Any help would be appreciated!

Last edited by jicco; 10-12-04 at 03:04.
Reply With Quote
  #2 (permalink)  
Old 10-12-04, 07:19
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
unless you're on mysql 4.1 and can use subqueries, i suggest you run separate queries

for next in group:
Code:
select min(product_id) 
  from Products_grouped
 where product_id >= 5
   and group_id = 1
where 5 and 1 are the values for the product you're currently sitting on

similar query for previous in group
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

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

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