Jump to content

SQL query to create new meta descriptions in spreadsheet


Chris2022

Recommended Posts

Hey Everyone,

I building a custom SQL query of a product name with id_product, id_lang, id_manufacturer, category, meta_description, brand name and multiple feature ids. This SQL query shows no result.

The goal is to creat a new meta description in the spreadsheet and import it to the database. In php, the query would be easier, but I have zero knowledge about it.

Here's my query:

SELECT pl.id_product, pl.id_lang, ml.id_manufacturer, p.active, pl.name as name_product, fp.id_feature as name_attribute, cl.name as category, m.name as brand, pl.meta_description
FROM pr_product p
LEFT JOIN pr_product_lang pl ON (p.id_product = pl.id_product)
LEFT JOIN pr_category_lang cl ON (cl.id_category = p.id_category_default and cl.id_lang = pl.id_lang)
LEFT JOIN pr_lang l on (l.id_lang = pl.id_lang)
LEFT JOIN pr_manufacturer_lang ml on (l.id_lang = pl.id_lang and l.id_lang = ml.id_lang)
LEFT JOIN pr_manufacturer m on (ml.id_manufacturer = m.id_manufacturer)
LEFT JOIN pr_feature_product fp on (pl.id_product = fp.id_product)
Where l.active = 1 AND pl.id_lang = 2 AND cl.name = 9 and fp.id_feature = 2 AND fp.id_feature = 3 AND fp.id_feature = 35 AND fp.id_feature = 39
Order by p.id_product, pl.id_lang, ml.id_manufacturer, fp.id_feature

 

Link to comment
Share on other sites

Thanks for the reply

I would like to get these columns from the query:

1514449172_columnsgoal.thumb.png.4bdccbbd141b31a222cbd401519f7ed1.png

 

So far, I was able to build such a query as a test. There are 15 records instead of one, and the features are 0-1 instead of the text

SELECT pl.id_product, cl.id_category, pl.id_lang, pl.name as product_name, cl.name as category, pf.id_feature = 13, pf.id_feature = 35, pf.id_feature = 2, pf.id_feature = 39, pf.id_feature = 3, m.name as brand, pl.meta_description, p.active
FROM pr_product p
LEFT JOIN pr_product_lang pl ON p.id_product = pl.id_product
LEFT JOIN pr_category_lang cl ON cl.id_category = p.id_category_default and cl.id_lang = pl.id_lang
Left Join pr_lang l on l.id_lang = pl.id_lang
LEFT JOIN pr_manufacturer_lang ml on (l.id_lang = pl.id_lang and l.id_lang = ml.id_lang)
LEFT JOIN pr_manufacturer m on (ml.id_manufacturer = m.id_manufacturer)
LEFT JOIN pr_feature_product pf ON p.id_product = pf.id_product 
LEFT JOIN pr_feature_lang fl ON (fl.id_feature = pf.id_feature AND fl.id_lang = 4)
LEFT JOIN pr_feature_value_lang fvl ON (fvl.id_feature_value = pf.id_feature_value AND fvl.id_lang = 4)
LEFT JOIN pr_feature f ON (f.id_feature = pf.id_feature AND fl.id_lang = 4)
INNER JOIN pr_feature_shop feature_shop
ON (feature_shop.id_feature = f.id_feature AND feature_shop.id_shop = 1)
where pl.id_lang = 4 and pf.id_product = 4
order by p.id_product, pl.id_lang

test.thumb.png.2b4cbb75ce3ed300e8d6b04f9a3bb67c.png

 

Thanks for the Update query, will be usefull. However, I have to show the columns in the workbook for approval to the client

 

 

 

Edited by Chris2022 (see edit history)
Link to comment
Share on other sites

Ok, sample:

obrazek.thumb.png.18d53bf286feeb634ac7d05b821f5608.png

SELECT 
pl.id_product /* ADD */as 'id_product', 
cl.id_category, 
pl.id_lang, 
pl.name as product_name, 
cl.name as category, 
/* NEW */
(
    SELECT x.value FROM pr_feature_value_lang x
    LEFT JOIN pr_feature_product y ON (y.id_product = id_product)
    WHERE x.id_lang = 4 AND y.id_feature = 13
) AS 'Surface Safety (id_feature = 13)', 
(
    SELECT x.value FROM pr_feature_value_lang x
    LEFT JOIN pr_feature_product y ON (y.id_product = id_product)
    WHERE x.id_lang = 4 AND y.id_feature = 35
) AS 'Kee Safety (id_feature = 35)', 
/*pf.id_feature = 13,
pf.id_feature = 35, 
pf.id_feature = 2, 
pf.id_feature = 39, 
pf.id_feature = 3, */
m.name as brand, 
pl.meta_description, 
p.active
FROM pr_product p
LEFT JOIN pr_product_lang pl ON p.id_product = pl.id_product
LEFT JOIN pr_category_lang cl ON cl.id_category = p.id_category_default and cl.id_lang = pl.id_lang
Left Join pr_lang l on l.id_lang = pl.id_lang
LEFT JOIN pr_manufacturer_lang ml on (l.id_lang = pl.id_lang and l.id_lang = ml.id_lang)
LEFT JOIN pr_manufacturer m on (ml.id_manufacturer = m.id_manufacturer)
/*LEFT JOIN pr_feature_product pf ON p.id_product = pf.id_product */
/*LEFT JOIN pr_feature_lang fl ON (fl.id_feature = pf.id_feature AND fl.id_lang = 4)*/
/*LEFT JOIN pr_feature_value_lang fvl ON (fvl.id_feature_value = pf.id_feature_value AND fvl.id_lang = 4)*/
/*LEFT JOIN pr_feature f ON (f.id_feature = pf.id_feature AND fl.id_lang = 4)*/
/*INNER JOIN pr_feature_shop feature_shopON (feature_shop.id_feature = f.id_feature AND feature_shop.id_shop = 1)*/
where pl.id_lang = 4 and pf.id_product = 4
order by p.id_product, pl.id_lang

 

Link to comment
Share on other sites

Thanks. Now I understand how to build subqueries. Only for a week I have been learning about SQL and the names of tables and columns from Presta :)

Presta shows such an error:

1958748225_prestaerror.thumb.jpg.4bd6493fcb2ac10a7bbeffa03c6e33d1.jpg

 

When build query without subqueries, with only one product id and one feature id, it displays correctly:

SELECT pl.id_product, cl.id_category, pl.id_lang, pl.name as product_name, cl.name as category, fvl.value, m.name as brand, pl.meta_description, p.active
FROM pr_product p
LEFT JOIN pr_product_lang pl ON p.id_product = pl.id_product
LEFT JOIN pr_category_lang cl ON cl.id_category = p.id_category_default and cl.id_lang = pl.id_lang
Left Join pr_lang l on l.id_lang = pl.id_lang
LEFT JOIN pr_manufacturer_lang ml on (l.id_lang = pl.id_lang and l.id_lang = ml.id_lang)
LEFT JOIN pr_manufacturer m on (ml.id_manufacturer = m.id_manufacturer)
LEFT JOIN pr_feature_product pf ON p.id_product = pf.id_product 
LEFT JOIN pr_feature_lang fl ON (fl.id_feature = pf.id_feature AND fl.id_lang = 4)
LEFT JOIN pr_feature_value_lang fvl ON (fvl.id_feature_value = pf.id_feature_value AND fvl.id_lang = 4)
LEFT JOIN pr_feature f ON (f.id_feature = pf.id_feature AND fl.id_lang = 4)
INNER JOIN pr_feature_shop feature_shop
ON (feature_shop.id_feature = f.id_feature AND feature_shop.id_shop = 1)
where pl.id_lang = 4 and pf.id_product = 4 and pf.id_feature = 13
order by p.id_product, pl.id_lang

 

I assume that it is about the correct add of the id shop

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...