Solved

Oder items from values from another table

Posted on 2012-04-13
8
193 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
 

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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

867 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now