Solved

Oder items from values from another table

Posted on 2012-04-13
8
197 Views
Last Modified: 2012-04-19
Hi again.

I have another problem and hope to get help from this community of great programmers.

I am trying to sort my products table from the values in my products_categories tables.

products table has:
psku
panme
pdescription

products_subcategories table has:
psku
subcatgorie_name
orderid

This setup allows me to add a product into multiple sub-categories. How could I sort the products_table based on values in my orderid from the products_subcategories table.

I have this but its not working at this point.

SELECT * FROM products LEFT JOIN products_subcategories ON products.psku = products_subcategories.psku WHERE products.psku = '6025' OR products.psku = '6022' OR products.psku = '6018' OR products.psku = '6017' GROUP BY products_subcategories.psku ORDER BY products_subcategories.dsporder ASC

Open in new window


The products show, but are not being sorted by orderid. The orderid is contain values from 1 - 4, where
psku #6025 has orderid 1
psku #6022 has orderid 2
psku #6018 has orderid 3
psku #6017 has orderid 4

On my page output, psku #6018 is first and #6025 is last, etc.

Any help would be appreciated.

Regards.
0
Comment
Question by:slickip
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 1

Expert Comment

by:jameless
ID: 37844084
Did you make a typo when asking the question?  Or did you copy and paste it from where you ran the code?

Try this:
SELECT 
	* 
FROM 
	products LEFT JOIN products_subcategories 
	ON products.psku = products_subcategories.psku 
WHERE products.psku IN ('6025','6022','6018','6017')
GROUP BY products_subcategories.psku 
ORDER BY products_subcategories.orderid ASC

Open in new window


Notice I condensed your OR into an IN.  Makes it a little easier on the eyes.
0
 

Author Comment

by:slickip
ID: 37844345
Hi jameless,

Thank you for replying and cleaner code example. I ran your code, but it produces the same result. It's still ignoring the sort by orderid. Changing the order behaviour from ASC to DESC shows on the result output, but the orderid is ignored.

Any more ideas on what the coding error could be?

Thanks.
0
 
LVL 1

Expert Comment

by:jameless
ID: 37844371
Whoops forgot to include it in the group by.  Try this?:
SELECT 
	* 
FROM 
	products LEFT JOIN products_subcategories 
	ON products.psku = products_subcategories.psku 
WHERE products.psku IN ('6025','6022','6018','6017')
GROUP BY products_subcategories.psku, products_subcategories.orderid 
ORDER BY products_subcategories.orderid ASC

Open in new window

0
Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

 

Author Comment

by:slickip
ID: 37844432
Hi again.
Thanks for the prompt reply.
I tried your code, but now I am getting duplicated psku's.
psku #6017 appears twice and so does psku #6022. Order looks like:

#6017
#6018
#6022
#6022
#6025
#6017

Thanks.
0
 
LVL 1

Expert Comment

by:jameless
ID: 37844545
Can #6022 have more than 1 orderid?  1,2,3,etc?
0
 

Author Comment

by:slickip
ID: 37844656
Hi again.

The reason for the double up is because #6022 is listed twice in the products_subcategories table. It's listed once for cosmetics and another time for eyelashes. Same with #6017. I believe it could have to do with the group by orderid part of the code. When removing that part, there are no dublicates.

Thanks.
0
 

Accepted Solution

by:
slickip earned 0 total points
ID: 37846206
Answering my own question I got it to work with the following:

$subcat = $_GET['subcategory'];
SELECT DISTINCT e2o_products.* FROM e2o_products
INNER JOIN e2o_products_subcategories 
ON e2o_products.psku = e2o_products_subcategories.psku 
WHERE e2o_products.pstatus =  '1' 
AND e2o_products_subcategories.subcategory = '$subcat'
AND e2o_products.psku IN ('6025','6022','6018','6017')
GROUP BY e2o_products_subcategories.psku
ORDER BY e2o_products_subcategories.dsporder ASC

Open in new window


This will return the data exactly how I am setting it through my orderid.

Thanks for anyone involved.
0
 

Author Closing Comment

by:slickip
ID: 37864751
Being a newbie in the field of PHP and MySQL, I was able to learn more on INNER JOIN, LEFT JOIN etc. I am sure that my solution isn’t the cleanest one, as I am certain this could be done way better. That’s why I am grading it as "Average"
0

Featured Post

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question