I'm trying to create a MySQL query that will list the products attributes on Prestashop together in a single row (I don't want to depend on paid modules).
The current query is:
SELECT
p.id_product AS 'idProduto',
pac.id_attribute,
al.name 'Nome_SKU',
pa.id_product_attribute as 'idSKU',
pl.name AS 'Nome',
pl.description_short AS 'Descricao Curta',
pl.description AS 'Descricao',
pl.meta_title AS 'Meta-Title',
pl.meta_keywords AS 'Meta-keyword',
pl.meta_description AS 'meta-description',
pl.link_rewrite AS 'URL rewrite',
pl.available_now AS 'texto_estoque',
pl.available_later AS 'texto_indisponivel',
pa.quantity as 'Quantidade',
p.price 'Preco',
pa.minimal_quantity 'Quantidade Minima',
p.active AS 'Ativo (0/1)'
FROM
ps_product p
INNER JOIN
ps_product_lang pl ON p.id_product = pl.id_product
INNER JOIN
ps_product_attribute pa ON pl.id_product = pa.id_product
INNER JOIN
ps_product_attribute_combination pac ON pa.id_product_attribute = pac.id_product_attribute
INNER JOIN
ps_attribute_lang al ON al.id_attribute = pac.id_attribute
where pl.id_lang = 6
AND p.id_product = 101
group by pa.id_product_attribute,al.name
And it's results are like this (i.e. id_product 101):
What I need is the results to "group" in a same row by their idSKU to show the values (Nome_SKU) each in one column, beeing like this (i.e. idSKU 647):
Related
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.
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
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.
I building a custom SQL query for my module to retrieve all combinations of a product with id_product and multiple attributes ids, but currently, I only managed to select it with one attribute and no more, I'm really missing something but didn't find it yet.
To get in context here's my query to find all combinations (color & size) of a product and its result:
SELECT
p.id_product,
pq.quantity,
pa.price AS price_diff,
p.price,
pai.id_image,
pl.name,
GROUP_CONCAT(agl.id_attribute_group, ':', pal.id_attribute ORDER BY agl.id_attribute_group SEPARATOR ", ") as combination_ids,
GROUP_CONCAT(pal.name ORDER BY agl.id_attribute_group SEPARATOR ", ") as combination
FROM ps_product p
LEFT JOIN ps_product_attribute pa ON (p.id_product = pa.id_product)
LEFT JOIN ps_stock_available pq ON (p.id_product = pq.id_product AND pa.id_product_attribute = pq.id_product_attribute)
LEFT JOIN ps_product_lang pl ON (p.id_product = pl.id_product)
LEFT JOIN ps_product_attribute_combination pac ON (pa.id_product_attribute = pac.id_product_attribute)
LEFT JOIN ps_attribute_lang pal ON (pac.id_attribute = pal.id_attribute)
LEFT JOIN ps_attribute a ON (pal.id_attribute = a.id_attribute)
LEFT JOIN ps_attribute_group_lang agl ON (a.id_attribute_group = agl.id_attribute_group)
LEFT JOIN ps_product_attribute_image pai on(pa.id_product_attribute = pai.id_product_attribute)
WHERE pl.id_lang = 1
AND pal.id_lang = 1
AND agl.id_lang = 1
AND p.id_product = 3196 -- My product
GROUP BY pac.id_product_attribute
The result
Query with a single attribute (size S for this example):
......................
......................
AND p.id_product = 3196 -- My product
AND agl.id_attribute_group = 9 -- size
AND pal.id_attribute = 761 -- 'S' size for my case
GROUP BY pac.id_product_attribute
But no success with specifying both size AND color, any idea?
I think you want a HAVING clause. To filter on two attributes, the logic would be:
SELECT ...
FROM ...
WHERE ...
GROUP BY pac.id_product_attribute
HAVING
MAX(agl.id_attribute_group = 9 AND pal.id_attribute = 761) = 1
AND MAX(agl.id_attribute_group = 2 AND pal.id_attribute = 727) = 1
I should warn that your code is not a valid aggregation query. You need more column in the GROUP BY clause to fix that flaw. It is hard to tell for sure without seeing your data, but, with a few assumptions on the primary key of each table:
GROUP BY
p.id_product,
pa.id_product_attribute,
pac.id_product_attribute,
pai.id_image,
pq.id -- if that exists?
It is only my second Query and I wonder, is there any chance to get what I need.
I'am working with Prestashop and I would like to export data about the product.
With the select "GROUP_CONCAT(DISTINCT ac.id_product_2)" I'am getting the product (which I need) ID, but I would like to get the product reference based on these ID.
I wonder, is there any change to get the product reference based on those ID?
My query is:
SELECT p.id_product, pl.name, p.reference, p.mcompleted,pl.description_short, pl.meta_title,
pl.link_rewrite, GROUP_CONCAT(DISTINCT ac.id_product_2)
FROM ps_product p
LEFT JOIN ps_product_lang pl ON (p.id_product = pl.id_product)
LEFT JOIN ps_stock_available s ON (p.id_product = s.id_product)
LEFT JOIN ps_accessory ac ON (ac.id_product_1 = p.id_product)
WHERE pl.id_lang = 2
AND p.active = 1
AND s.quantity > 0
AND p.id_product > 35000
AND p.mcompleted = 1
GROUP BY p.id_product
Right know I'am getting the data something like this:
id_product reference GROUP_CONCAT(DISTINCT ac.id_product_2)
35026 21-176F 35026,35027
35027 55-FFFF 35027,35028
35028 66-FFFF 35026,35028
But I would like to get something like this:
id_product reference GROUP_CONCAT(DISTINCT ac.id_product_2)
35026 21-176F 21-176F,55-FFFF
35027 55-FFFF 55-FFFF,66-FFFF
35028 66-FFFF 21-176F,66-FFFF
Already spent 6+ hours on internet, hoping for Your help :)
Assuming the ac.id_product_2 also links to the ps_product table then you could simple make another join and concatenate the reference instead of the id.
SELECT p.id_product, pl.name, p.reference, p.mcompleted,pl.description_short, pl.meta_title,
pl.link_rewrite, GROUP_CONCAT(DISTINCT p2.reference)
FROM ps_product p
LEFT JOIN ps_product_lang pl ON (p.id_product = pl.id_product)
LEFT JOIN ps_stock_available s ON (p.id_product = s.id_product)
LEFT JOIN ps_accessory ac ON (ac.id_product_1 = p.id_product)
LEFT JOIN ps_product p2 on (p2.id_product = ac.id_product_2)
WHERE pl.id_lang = 2
AND p.active = 1
AND s.quantity > 0
AND p.id_product > 35000
AND p.mcompleted = 1
GROUP BY p.id_product
I have the following code which I'm using to get prices from a Magento database:
SELECT
`e`.`sku`,
`price_index`.`price` AS `RRP`,
`price_index`.`final_price` AS `Dealer Price`
FROM
`catalog_product_entity` AS `e`
INNER JOIN
`catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND
price_index.website_id = '1' AND price_index.customer_group_id = 7
This works fine, however I need to get an additional price from "final_price" but with customer group 8 instead of 7 for example.
The results from my current script display:
SKU, RRP, Dealer Price
The Results I would like:
SKU, RRP, Dealer Price, Trader Price
Thanks
You just need left joins instead of inner joins. Note in the tested example below that I'm renaming the alias of price_index to price_index2 in the second join on the same table.
SELECT
`e`.`sku`,
`price_index`.`price` AS `RRP`,
`price_index`.`final_price` AS `Dealer Price`,
`price_index2`.`final_price` AS `Trader Price`
FROM
`catalog_product_entity` AS `e`
LEFT JOIN
`catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND
price_index.website_id = '1' AND price_index.customer_group_id = 7
LEFT JOIN
`catalog_product_index_price` AS `price_index2` ON price_index2.entity_id = e.entity_id AND
price_index2.website_id = '1' AND price_index2.customer_group_id = 8
In my shop the Product A has different attributes, let them be:
White (12pcs left), Black (10 left) and Red (6 left)
I have managed to export into csv all products with their categories and quantities and attributes, but I would like the table to look as follows:
Product A / White / 12
Product A / Black / 10
Product A / Red / 6
Product B / Size S / 5
Product B / Size M / 6
So all the quantities available for each combination of the product.
My query now is as follows:
SELECT
p.id_product,
p.id_category_default,
GROUP_CONCAT(DISTINCT(cl.name) SEPARATOR ";") as categories,
pa.reference,
pl.name,
GROUP_CONCAT(DISTINCT(pal.name) SEPARATOR "; ") as combination,
p.price,
pq.quantity
FROM 7_ps_product p
LEFT JOIN 7_ps_product_attribute pa ON (p.id_product = pa.id_product)
LEFT JOIN 7_ps_stock_available pq ON (p.id_product = pq.id_product AND pa.id_product_attribute = pq.id_product_attribute)
LEFT JOIN 7_ps_product_lang pl ON (p.id_product = pl.id_product)
LEFT JOIN 7_ps_product_attribute_combination pac ON (pa.id_product_attribute = pac.id_product_attribute)
LEFT JOIN 7_ps_attribute_lang pal ON (pac.id_attribute = pal.id_attribute)
LEFT JOIN 7_ps_category_product cp ON (p.id_product = cp.id_product)
LEFT JOIN 7_ps_category_lang cl ON (cp.id_category = cl.id_category)
LEFT JOIN 7_ps_category c ON (cp.id_category = c.id_category)
WHERE pl.id_lang = 1
AND pal.id_lang = 1
GROUP BY pa.reference
ORDER BY p.id_product, pac.id_attribute
Thanks for great contributions to help out people like me!!
I have changed your query like this, so to get the output you need
SELECT
CONCAT(pl.name,' / ',pal.name,' / ', pq.quantity) AS required
FROM ps_product p
LEFT JOIN ps_product_attribute pa ON (p.id_product = pa.id_product)
LEFT JOIN ps_stock_available pq ON (p.id_product = pq.id_product AND pa.id_product_attribute = pq.id_product_attribute)
LEFT JOIN ps_product_lang pl ON (p.id_product = pl.id_product)
LEFT JOIN ps_product_attribute_combination pac ON (pa.id_product_attribute = pac.id_product_attribute)
LEFT JOIN ps_attribute_lang pal ON (pac.id_attribute = pal.id_attribute)
LEFT JOIN ps_category_product cp ON (p.id_product = cp.id_product)
LEFT JOIN ps_category_lang cl ON (cp.id_category = cl.id_category)
LEFT JOIN ps_category c ON (cp.id_category = c.id_category)
WHERE pl.id_lang = 1
AND pal.id_lang = 1
GROUP BY p.id_product,pal.name
ORDER BY p.id_product, pac.id_attribute
I have changed the GROUP BY and SELECT fields. I think this is the answer you need.