Solved

Oder items from values from another table

Posted on 2012-04-13
8
196 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hide and Unhide Table 6 27
PHP Underscores vs. Directory separators 5 29
How do I remove "" from json_encode 5 23
Boolean 13 28
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

730 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