Jump to content

Recommended Posts

hi, we only have about 120 entries within the order table yet this join query is pulling in over 500.  Where did I goof? 

 

SELECT orders.orderid 
	, orders.cartid 
	, skus.skuid 
	, skus.price 
	, products.productid 
	, products.name  
FROM orders 
	INNER JOIN items ON 
		(items.cartid = orders.cartid) 
	INNER JOIN products ON 
		(products.productid = items.productid) 
	INNER JOIN skus ON 
		(skus.productid = products.productid)

Link to comment
https://forums.phpfreaks.com/topic/189891-mysql-inner-join/
Share on other sites

INNER JOIN in MySQL is equivalent to CROSS JOIN, which means that if you have 2 rows in table A

 

A:

ID

--

1,

2,

 

and three rows in table B referencing table A

 

B:

ID, AID

--

1,1

2,1

3,2

 

You will get three rows all together from A INNER JOIN B ON A.ID = B.AID

 

Link to comment
https://forums.phpfreaks.com/topic/189891-mysql-inner-join/#findComment-1001974
Share on other sites

so instead of

 

SELECT orders.orderid 
	, orders.cartid 
	, skus.skuid 
	, skus.price 
	, products.productid 
	, COUNT(products.productid) AS productOrders 
	, products.name  
FROM orders 
	INNER JOIN items ON 
		(items.cartid = orders.cartid) 
	INNER JOIN products ON 
		(products.productid = items.productid) 
	INNER JOIN skus ON 
		(skus.productid = products.productid)
			GROUP BY orders.orderid 
				ORDER BY productOrders DESC 
					LIMIT 10

 

do this?

SELECT orders.orderid 
	, orders.cartid 
                , MAX(orders.cartid) AS maxOrders
	, skus.skuid 
	, skus.price 
	, products.productid 
	, COUNT(products.productid) AS productOrders 
	, products.name  
FROM orders 
	INNER JOIN items ON 
		(items.cartid = orders.cartid) 
	INNER JOIN products ON 
		(products.productid = items.productid) 
	INNER JOIN skus ON 
		(skus.productid = products.productid)
			GROUP BY orders.orderid 
				ORDER BY productOrders DESC 
					LIMIT 10

 

?

Link to comment
https://forums.phpfreaks.com/topic/189891-mysql-inner-join/#findComment-1002122
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.