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 > Query help: Recursive + joins

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-21-11, 03:00
RGM RGM is offline
Registered User
 
Join Date: Feb 2011
Posts: 29
Query help: Recursive + joins

I'm having a little trouble getting the data I need for related products.

Given a product id of 1 I want to find all the related products (who's ID is in related_id) then get their full info from the products table and join with the brands table to get the name.

To get just the product and brand without the related I've been using:

PHP Code:
SELECT `p`.*, `b`.`name` as brand_name 
FROM 
(`QS_productsp
JOIN `QS_brandsb ON `p`.`brand_id` = `b`.`id`
WHERE `p`.`id` = and p.visible "yes" 
Attached Thumbnails
Query help: Recursive + joins-grab.jpg  
Reply With Quote
  #2 (permalink)  
Old 02-21-11, 05:59
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Code:
SELECT p.*
     , b.name as brand_name  
     , p2.id AS related_id
     , b2.name AS related_brand
  FROM QS_products AS p 
INNER 
  JOIN QS_brands AS b 
    ON b.id = p.brand_id
LEFT OUTER
  JOIN QS_related_products AS r
    ON r.product_id = p.id
LEFT OUTER
  JOIN QS_products AS p2
    ON p2.id = r.related_id 
   AND p2.visible = 'yes'  
LEFT OUTER
  JOIN QS_brands AS b2
    ON b2.id = p2.brand_id
 WHERE p.id = 1 
   AND p.visible = 'yes'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-21-11, 14:44
RGM RGM is offline
Registered User
 
Join Date: Feb 2011
Posts: 29
Thanks for the reply Rudy.

I changed it a little to return the right results:

PHP Code:
SELECT 
      p2
.*
    , 
b2.name AS related_brand
 FROM QS_products 
AS 
INNER 
 JOIN QS_brands 
AS 
   ON b
.id p.brand_id
LEFT OUTER
 JOIN QS_products_related 
AS r
   ON r
.product_id p.id
LEFT OUTER
 JOIN QS_products 
AS p2
   ON p2
.id r.related_id 
  
AND p2.visible 'yes'  
LEFT OUTER
 JOIN QS_brands 
AS b2
   ON b2
.id p2.brand_id
WHERE p
.id 
  
AND p.visible 'yes' 
Note the p2.* is for brevity's sake.

Looks quite laborious though? I'd be interested in a better solution if you have any opinions for one.

The other option I had for this was a 'related' field in the products table with a csv line of id's (2,3,4,5,6), the query being:

PHP Code:
select p.*, `b`.`name` as brand_name  
from QS_products p
JOIN 
`QS_brandsb ON `p`.`brand_id` = `b`.`id`
where p.id in (2,3,4,5,6) and p.visible "yes"
The 2,3,4,5,6 being a variables value. From searching here you seem to not recommend the IN() approach however?
Reply With Quote
  #4 (permalink)  
Old 02-21-11, 15:25
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by RGM View Post
The 2,3,4,5,6 being a variables value.
which you could only have got from a previous query

so you're running two queries, right? compared to my one?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 02-21-11, 15:32
RGM RGM is offline
Registered User
 
Join Date: Feb 2011
Posts: 29
Correct, I have the full row for the current product (id=1) before I run this one for related products.

Sorry i should have made that clear. I run four in total:

- current product row from 'QS_products'
- related images
- related options
- and this one, related products


This image verification is painful!
Reply With Quote
  #6 (permalink)  
Old 02-21-11, 18:57
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by RGM View Post
This image verification is painful!
huh? image verification?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 02-21-11, 20:04
RGM RGM is offline
Registered User
 
Join Date: Feb 2011
Posts: 29
Us new posters have to suffer through an Image verification captcha to post..

So would you recommend the IN() method or?
Reply With Quote
  #8 (permalink)  
Old 02-21-11, 20:15
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by RGM View Post
So would you recommend the IN() method or?
sure, if you're okay with running separate queries, yes
__________________
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