Jump to content

Recommended Posts

Hello there.

Currently I have 3 tables and i'd like to make it one query for the ProductID. Here are my tables:

 

Saved_product:

ProductID, email, colour, initials, shape, profile_name

 

profiles:

profile_name, email, opt1, opt2, opt3, opt4

(there is no primary key, but the profile_name is unique for that email/user. I realize this is a bad design, but i used a chinese web design company)

 

ordered_products:

OrderID, ProductID, quantity, total

 

 

What i would like to do (in english) is this:

 

Selected all from saved_product, ordered_products and profiles WHERE productID = 1111002

The problem i have is the 'profiles' table does not had productID inside of it. It only has the email. So i need to look at the 'ordered_products' to find what profile name and email address to look for.

 

At the moment i do it with these two queries. But i need it to be one query depending on the selected productID

"SELECT * from profiles WHERE email = '$email' AND profile_name = '$profile''";

"SELECT * FROM Saved_product INNER JOIN ordered_products ON Saved_product. ProductID = ordered_products. ProductID WHERE Saved_product. ProductID = '$ID'";

 

 

please help!

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/94884-join-3-tables-with-one-query/
Share on other sites

Give this a go

 

SELECT *
FROM Saved_product, profiles, ordered_products
WHERE Saved_product.ProductID = ordered_products.ProductID
  AND Saved_product.email = profiles.email
  AND Saved_product.ProductID = 1111002

 

You may need to use:

SELECT Saved_product.*, profiles.*, ordered_products. *

in the future i will re-write it so that the profile table has a unique ID.

 

But is it possible to make a proper join without doing that? I'm not entirely sure what a proper join is, my sql knowledge is low! :)

 

Thanks a lot for the code fnairb, i will try it now!

I'm not sure what a "proper" join is either.  The query I provided joins the tables implicitly.  True, it does not explicitly use a JOIN keyword.  maybe that is what fenway is referring too.  I would love to see how that query would be written so I could use it in the future.

The query I provided joins the tables implicitly.  True, it does not explicitly use a JOIN keyword.

"Implicitly" is precisely the problem:

 

SELECT *
FROM Saved_product AS sp 
INNER JOIN profiles AS p ON ( p.email = sp.email ) 
INNER JOIN ordered_products AS op ON ( sp.ProductID = op.ProductID ) 
WHERE sp.ProductID = 1111002

This will keep you out of trouble.

If I get your question all you need to do is replace the

ProductID = 1111002

 

with

email = 'whatever' and profile_name = 'whatever'

 

Using the join that would look like:

SELECT *
FROM Saved_product AS sp 
INNER JOIN profiles AS p ON ( p.email = sp.email ) 
INNER JOIN ordered_products AS op ON ( sp.ProductID = op.ProductID ) 
WHERE sp.email = 'whatever'
  AND sp.profile_name = 'whatever'

 

The other would look like:

SELECT *
FROM Saved_product, profiles, ordered_products
WHERE Saved_product.ProductID = ordered_products.ProductID
  AND Saved_product.email = profiles.email
  AND Saved_product.email = 'whatever'
  AND Saved_product.profile_name = 'whatever'

what is the difference/advantages between the two codes?

It's much better to use JOIN -- no issues with comma precedence changes, no missing join conditions, clear where clauses for filtering only, etc.  Also, makes it trivial to switch join type (inner vs left) in a second.

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.