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
https://forums.phpfreaks.com/topic/40428-join-3-tables/
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
https://forums.phpfreaks.com/topic/40428-join-3-tables/#findComment-196026
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
https://forums.phpfreaks.com/topic/40428-join-3-tables/#findComment-196266
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.