Jump to content

Join 3 Tables


The Little Guy

Recommended Posts

OK.. Im trying to join 3 tables PRODUCTS, IMAGES, and VIEWED_PRODUCTS.

 

Table: VIEWED_PRODUCTS

IP  	                       SESSION  	      DATE  	           PRODUCT  	       ID
68.117.46.68 	8fj2fd8c9sffc5k994iutcl9r4 	2007-02-24 22:41:12 	     3 	               1

 

That is one row in the table, and I need everything to be based off of PRODUCT and ID from the other 2 tables.

So... How do I get my code to work?

 

According to my above table:

- VIEWED_PRODUCTS.ID = 1

- PRODUCTS.PRODUCT_NUM = 3

- IMAGES.PRODUCT_NUM = 3

 

When I run the SQL, I get no output, not even an error.

 

SELECT PRODUCTS . * , IMAGES . * , VIEWED_PRODUCTS . *
FROM PRODUCTS, IMAGES, VIEWED_PRODUCTS
WHERE VIEWED_PRODUCTS.ID = '1'
AND PRODUCTS.PRODUCT_NUM = VIEWED_PRODUCTS.PRODUCT
AND IMAGES.PRODUCT_NUM = VIEWED_PRODUCTS.PRODUCT
ORDER BY VIEWED_PRODUCTS.DATE
LIMIT 3

Link to comment
Share on other sites

And you really shouldn't be using all uppercase for queries... artacus means something like:

 

SELECT p.* , i.* , vp.*
FROM viewed_products AS vp
LEFT JOIN products AS p ON ( p.product_num = vp.product  ) 
LEFT JOIN images as i ON ( i.product_num = p.product_num )
WHERE vp.id = '1'
ORDER BY vp.date 
LIMIT 3

 

I'm guessing you might not want left joins everywhere, I don't know what you're trying to do.

Link to comment
Share on other sites

Well... I think I got it to work, atleast so far it works.

 

Here is what I did anyways:

SELECT *
FROM PRODUCTS
INNER JOIN IMAGES
INNER JOIN VIEWED_PRODUCTS
ON 
PRODUCTS.PRODUCT_NUM = VIEWED_PRODUCTS.PRODUCT
WHERE
VIEWED_PRODUCTS.ID = '{$_SESSION['id']}'
AND 
IMAGES.PRODUCT_NUM = VIEWED_PRODUCTS.PRODUCT
AND
IMAGES.MAIN = '1'
ORDER BY VIEWED_PRODUCTS.DATE DESC LIMIT 3

 

Thanks for the help

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.