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
Related
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?
So I'm having a slight problem with having to save price on a product in two different tables due to a few reasons. Is it possible to merge two columns into one? I know UNION exists but does it work with LEFT JOIN's?
Any pointers is much appreciated.
Best Regards
SELECT
si.id AS shop_item_id,
si.item_price,
s.logo_file_name,
p.cat_id AS category_id,
api.item_price AS api_price,
MAX(c.campaign_desc) AS campaignDesc,
MAX(c.campaign_type_id) AS campaignType,
MAX(c.shop_id) AS campaign_shop_id,
MAX(ct.image_name) AS campaignLogo
FROM
shop_item si
LEFT JOIN
shop s ON
s.id = si.shop_id
LEFT JOIN
product p ON
si.product_id = p.id
LEFT JOIN
campaign_category cc ON
cc.category_id = p.cat_id
LEFT JOIN
campaign c ON
c.id = cc.campaign_id AND
c.shop_id = si.shop_id AND
c.show_in_pricetable = 1 AND
NOW() BETWEEN c.date_from and c.date_to
LEFT JOIN
campaign_type ct ON
c.campaign_type_id = ct.id
LEFT JOIN
shop_api_item api ON
si.rel_feed_api = api.unique_id AND si.shop_id = api.shop_id
WHERE
si.`product_id` = 586 AND
s.`active_shop` = 1
GROUP BY
s.name,
si.id ,
si.item_price
ORDER BY
si.`item_price`,
si.`shop_id`,
c.`campaign_desc` DESC
It looks like you would benefit from the COALESCE() function.
SELECT
si.id AS shop_item_id,
COALESCE(si.item_price, api.item_price) AS coalesced_price,
...
COALESCE() takes multiple arguments, and returns the first argument that is not NULL.
I have 2 tables a car model and car parts. Both are related with a partfitment table whose only fields are the primary keys modelID and partID.
I am trying to display the parts that fit the model car I selected.
The following works fine:
SELECT tblmodel.modelID,
tblmodel.model,
tblparts.part,
tblparts.part_number,
tblparts.description,
tblparts.list_price
FROM (tblmodel
INNER JOIN tblpartfitment
ON tblmodel.modelID = tblpartfitment.modelID)
INNER JOIN tblparts
ON tblpartfitment.partID = tblparts.partID
WHERE tblmodel.modelID = 1;
The tblparts table has the tinyint field named universal_part. I am trying to include all the parts that have universal_part = true in my original query.
In other words always return all parts that are universal and those that are for the modelID specified.
I tried using a union, but get errors.
I think you can just include the logic in the ON clause:
SELECT m.ModelID, m.Model, p.part, p.part_number, p.description, p.list_price
FROM tblmodel m INNER JOIN
tblpartfitment pf
ON m.ModelID = pf.modelID INNER JOIN
tblparts p
ON pf.partID = p.partID OR p.universal_part = 1
WHERE m.ModelID = 1;
EDIT:
You may be best off using union:
SELECT m.ModelID, m.Model, p.part, p.part_number, p.description, p.list_price
FROM tblmodel m INNER JOIN
tblpartfitment pf
ON m.ModelID = pf.modelID INNER JOIN
tblparts p
ON pf.partID = p.partID AND p.universal_part <> 1
WHERE m.ModelID = 1
UNION ALL
SELECT m.ModelID, m.Model, p.part, p.part_number, p.description, p.list_price
FROM tblmodel m CROSS JOIN
tblparts p
WHERE p.universal_part = 1;
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):
I have the SQL to display ALL the activities and relative Admin permissions (if any) for that activity.
Current SQL Code:
SELECT `activities`.*, `admins`.`admin_role_id`
FROM (`activities`)
LEFT JOIN `admins` ON `admins`.`activity_id`=`activities`.`id` AND admins.member_id=27500
WHERE `activities`.`active` = 1
Returning:
id | name | description | active | admin_role_id (or null)
I then need to detect whether they are an active member within that Activity.
I have the following SQL code:
SELECT DISTINCT `products`.`activity_ID` as joinedID
FROM (`transactions_items`)
JOIN `transactions` ON `transactions`.`id` = `transactions_items`.`id`
JOIN `products` ON `products`.`id` = `transactions_items`.`product_id`
JOIN `activities` ON `activities`.`id` = `products`.`activity_ID`
WHERE `transactions`.`member_id` = 27500
AND `activities`.`active` = 1
Is there any way to merge this into one SQL query. I can't figure out how to use the correct JOIN queries, because of the complexity of the JOINs.
Help please, thanks! :)
Try like this
SELECT `activities`.*, `admins`.`admin_role_id`
FROM (`activities`)
LEFT JOIN `admins` ON `admins`.`activity_id`=`activities`.`id` AND admins.member_id=27500
JOIN (`transactions_items`
JOIN `transactions` ON `transactions`.`id` = `transactions_items`.`id`
JOIN `products` ON `products`.`id` = `transactions_items`.`product_id`)
ON `activities`.`id`=`products`.`activity_ID`
WHERE `transactions`.`member_id` = 27500
AND `activities`.`active` = 1
Seems to me that a query like this would be marginally more comprehensible and (I think) adhere more closely to the spec...
SELECT c.*
, d.admin_role_id
FROM activities c
LEFT
JOIN admins d
ON d.activity_id = c.id
AND d.member_id = 27500
LEFT
JOIN products p
ON p.activity_ID = c.id
LEFT
JOIN transactions_items ti
ON ti.product_id = p.id
LEFT
JOIN transactions t
ON t.id = ti.id
AND t.member_id = 27500
WHERE c.active = 1