Solved

Oder items from values from another table

Posted on 2012-04-13
8
195 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
  • 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

860 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