Post by Joe1234 » Tue Apr 16, 2024 12:43 pm

I have a query for a feed:

Code: Select all

	$sql = "SELECT DISTINCT p.sku as id, p.model, p.image AS image_link, i.image AS additional_image_link, p.status AS availability, p.date_available AS availability_date, p.weight AS shipping_weight, p.price, s.price as sale_price, d.name as title, d.description, m.name AS brand, u.keyword as link
		 , SUBSTRING_INDEX(group_concat(CASE a.attribute_id WHEN '34' THEN a.text END), ',', 1) `condition`
		 , SUBSTRING_INDEX(group_concat(CASE a.attribute_id WHEN '35' THEN a.text END), ',', 1) `gender`
		 , SUBSTRING_INDEX(group_concat(CASE a.attribute_id WHEN '36' THEN a.text END), ',', 1) `size`
		 , SUBSTRING_INDEX(group_concat(CASE a.attribute_id WHEN '37' THEN a.text END), ',', 1) `color1`
		 , SUBSTRING_INDEX(group_concat(CASE a.attribute_id WHEN '38' THEN a.text END), ',', 1) `color2`
		 , SUBSTRING_INDEX(group_concat(CASE a.attribute_id WHEN '41' THEN a.text END), ',', 1) `material`
	  FROM roottesCart_product_attribute a
		LEFT JOIN roottesCart_product p ON p.product_Id = a.product_id
		LEFT JOIN roottesCart_product_special s ON p.product_id = s.product_id
		LEFT JOIN roottesCart_product_description d ON p.product_id = d.product_id
		LEFT JOIN roottesCart_manufacturer m ON p.manufacturer_id = m.manufacturer_id
		LEFT JOIN roottesCart_product_image i ON p.product_id = i.product_id
		LEFT JOIN roottesCart_seo_url u ON CONCAT('product_id=',p.product_Id) = u.query
	  WHERE p.model <> 'Sweepstakes' AND p.quantity > 0 AND p.status = 1
	  GROUP BY a.product_id;";
I need to LEFT JOIN the highest value category_id that the product is attached to (not really the best way...and probably not future proof, but the best way I can see to accomplish my needs with my store). So I came up with this

Code: Select all

SELECT c.product_id, c.category_id
FROM roottesCart_product_to_category c
INNER JOIN (
    SELECT product_id, MAX(category_id) AS max_category
    FROM roottesCart_product_to_category
    GROUP BY product_id
) AS subq ON c.product_id = subq.product_id AND c.category_id = subq.max_category;
The problem is I can't figure out how to add this to the main query. Any assistance please.
Last edited by Joe1234 on Thu Apr 18, 2024 1:50 am, edited 1 time in total.

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by paulfeakins » Tue Apr 16, 2024 9:32 pm

I asked ChatGPT for you but didn't test it, use at your own risk:
ChatGPT wrote: To incorporate the second query into the main query for the product feed, you can use a subquery that finds the highest category_id for each product as you've designed, and then perform a LEFT JOIN with this result. The goal is to integrate the results of finding the maximum category_id per product into the main product query.

Here's how you can modify your main query to include the category_id from the highest value per product:

Subquery for Highest category_id: As you've created, this subquery will calculate the maximum category_id for each product.

LEFT JOIN with Main Query: Add this subquery to the main selection query as an additional left join. This will attach the maximum category_id to each product entry, which can then be selected as part of the main query's fields.

Here is the modified SQL incorporating these changes:

Code: Select all

SELECT 
    DISTINCT 
    p.sku as id, 
    p.model, 
    p.image AS image_link, 
    i.image AS additional_image_link, 
    p.status AS availability, 
    p.date_available AS availability_date, 
    p.weight AS shipping_weight, 
    p.price, 
    s.price as sale_price, 
    d.name as title, 
    d.description, 
    m.name AS brand, 
    u.keyword as link,
    max_cat.category_id AS highest_category_id,  -- Selecting the highest category_id
    SUBSTRING_INDEX(group_concat(CASE a.attribute_id WHEN '34' THEN a.text END), ',', 1) `condition`,
    SUBSTRING_INDEX(group_concat(CASE a.attribute_id WHEN '35' THEN a.text END), ',', 1) `gender`,
    SUBSTRING_INDEX(group_concat(CASE a.attribute_id WHEN '36' THEN a.text END), ',', 1) `size`,
    SUBSTRING_INDEX(group_concat(CASE a.attribute_id WHEN '37' THEN a.text END), ',', 1) `color1`,
    SUBSTRING_INDEX(group_concat(CASE a.attribute_id WHEN '38' THEN a.text END), ',', 1) `color2`,
    SUBSTRING_INDEX(group_concat(CASE a.attribute_id WHEN '41' THEN a.text END), ',', 1) `material`
FROM 
    roottesCart_product_attribute a
LEFT JOIN 
    roottesCart_product p ON p.product_Id = a.product_id
LEFT JOIN 
    roottesCart_product_special s ON p.product_id = s.product_id
LEFT JOIN 
    roottesCart_product_description d ON p.product_id = d.product_id
LEFT JOIN 
    roottesCart_manufacturer m ON p.manufacturer_id = m.manufacturer_id
LEFT JOIN 
    roottesCart_product_image i ON p.product_id = i.product_id
LEFT JOIN 
    roottesCart_seo_url u ON CONCAT('product_id=',p.product_Id) = u.query
LEFT JOIN (
    SELECT c.product_id, c.category_id
    FROM roottesCart_product_to_category c
    INNER JOIN (
        SELECT product_id, MAX(category_id) AS max_category
        FROM roottesCart_product_to_category
        GROUP BY product_id
    ) AS subq ON c.product_id = subq.product_id AND c.category_id = subq.max_category
) AS max_cat ON p.product_id = max_cat.product_id  -- Left join the subquery for max category
WHERE 
    p.model <> 'Sweepstakes' 
    AND p.quantity > 0 
    AND p.status = 1
GROUP BY 
    a.product_id;
This modified SQL query includes the original product details and extends it with the highest_category_id, representing the highest category_id for each product. This adjustment ensures that all the necessary joins are correctly applied and all fields are available for selection in the final output.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by Joe1234 » Tue Apr 16, 2024 10:29 pm

Thanks, I'm not able to test it yet, but when you asked, exactly what was your phrasing? I couldn't get anywhere with using AI on this....I was using Bing's copilot though.

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by straightlight » Tue Apr 16, 2024 11:39 pm

Joe1234 wrote:
Tue Apr 16, 2024 10:29 pm
Thanks, I'm not able to test it yet, but when you asked, exactly what was your phrasing? I couldn't get anywhere with using AI on this....I was using Bing's copilot though.
Here's a simple example: https://stackoverflow.com/a/15667227

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by paulfeakins » Wed Apr 17, 2024 10:24 pm

Joe1234 wrote:
Tue Apr 16, 2024 10:29 pm
Thanks, I'm not able to test it yet, but when you asked, exactly what was your phrasing? I couldn't get anywhere with using AI on this....I was using Bing's copilot though.
I've got the paid ChatGTP Plus using version 4. I just pasted your question as it was.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by Joe1234 » Thu Apr 18, 2024 1:50 am

OK, thanks.

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am
Who is online

Users browsing this forum: No registered users and 18 guests