Solved

Oder items from values from another table

Posted on 2012-04-13
8
191 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Expert Comment

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

Author Comment

by:slickip
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This article discusses four methods for overlaying images in a container on a web page
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…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

763 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

5 Experts available now in Live!

Get 1:1 Help Now