0

I am working with Prestashop 1.6.0.6, it appears to have a bug where randomly the virtual product file disappears and is no longer available for download and needs to be re-uploaded. So I am trying to write a query that will display all active products that have virtual products and whether that virtual product is active. In some instances since I have uploaded a second time the virtual product is active can be 0 or 1, by summing I can get the final active status.

I have found all the tables I need to write the query;

ps_product_lang->id_product
ps_product_lang->name

ps_product->id_product
ps_product->is_virtual
ps_product->active

ps_product_download->id_product
ps_product_download->display_filename
ps_product_download->filename
ps_product_download->active

I have limited mySQL query knowledge so normally write the query in MS Accesss then use the SQL view and copy to mySQL and normally it works with a little tweaking. In this instance I am using grouping, first and summing (is virtual product active) and can't get the query to work. I keep getting syntax errors. This is the query I am using;

SELECT ps_product_lang.id_product, 
       First(ps_product_lang.name) AS FirstOfname, 
       First(ps_product.is_virtual) AS FirstOfis_virtual, 
       First(ps_product.active) AS FirstOfactive, 
       First(ps_product_download.display_filename) AS FirstOfdisplay_filename, 
       First(ps_product_download.filename) AS FirstOffilename, 
       Sum(ps_product_download.active) AS SumOfactive
FROM (ps_product 
RIGHT JOIN ps_product_lang ON ps_product.id_product = ps_product_lang.id_product) 
LEFT JOIN ps_product_download ON ps_product.id_product = ps_product_download.id_product
GROUP BY ps_product_lang.id_product
HAVING (((First(ps_product.is_virtual))=1) AND ((First(ps_product.active))=1));

Any help to get the query working would be appreciated.

1
  • I don't think there's an implementation for FIRST() in mysql. You also should remove the paranthesis from FROM (ps_product Commented Nov 9, 2014 at 12:18

1 Answer 1

2

First, rewrite the query using table aliases and fixing the outer joins in the from clause. I find it quite hard to understand the logic with queries mix outer join types. Because the query has columns in the product table in the having clause, the first join is really an inner join anyway. And, because the second join uses the product table and not the product lang table, this is also an inner join. This is the structure of your query:

SELECT p.id_product, 
       First(l.name) AS FirstOfname, 
       First(p.is_virtual) AS FirstOfis_virtual, 
       First(p.active) AS FirstOfactive, 
       First(pd.display_filename) AS FirstOfdisplay_filename, 
       First(pd.filename) AS FirstOffilename, 
       Sum(pd.active) AS SumOfactive
FROM ps_product_lang l JOIN
     ps_product p
     ON p.id_product = l.id_product JOIN
     ps_product_download pd
     ON p.id_product = pd.id_product
GROUP BY l.id_product
HAVING First(p.is_virtual) = 1) AND First(p.active) = 1;

I don't fully understand the data structure. If I were to assume that product.id_product is a unique key (which would be a normal naming convention), then you do not need any aggregation functions on things for that table. However, your question suggests that this is not unique.

You can implement first using a trick in MySQL, combining substring_index() and group_concat().

However, to get you started, I think group_concat() might be more helpful. So, I would recommend that you start with this query:

SELECT l.id_product, 
       group_concat(l.name) AS allnames, 
       group_concat(p.is_virtual) AS allis_virtuals, 
       group_concat(p.active) AS allactives, 
       group_concat(pd.display_filename) AS alldisplay_filenames, 
       group_concat(pd.filename) AS allfilenames, 
       Sum(pd.active) AS SumOfactive
FROM ps_product_lang l JOIN
     ps_product p
     ON p.id_product = l.id_product JOIN
     ps_product_download pd
     ON p.id_product = pd.id_product
GROUP BY l.id_product
HAVING SUM(p.is_virtual = 1) > 0 AND SUM(p.active = 1) > 0;

If this doesn't do what you want, then you should be able to tweak it.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for this, does seem to work. But you have a typo error in the query, line 3 "conat" rather than "concat" - update: Have edited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.