complex query with joins, how to retrieve COUNTs - mysql

I have a rather complex query to retrieve products & attributes from several tables.
SELECT SQL_CALC_FOUND_ROWS
p.*,
product_shop.*,
product_shop.id_category_default,
pl.*,
pbn.*,
MAX(image_shop.id_image) id_image,
il.legend,
m.name manufacturer_name,
0 as quantity
FROM ps_category_product cp
LEFT JOIN ps_category c ON (c.id_category = cp.id_category)
LEFT JOIN ps_product p ON p.id_product = cp.id_product
INNER JOIN ps_product_shop product_shop ON (product_shop.id_product = p.id_product AND product_shop.id_shop = 1)
LEFT JOIN ps_product_lang pl ON (pl.id_product = p.id_product AND pl.id_shop = 1 AND pl.id_lang = 7)
## ########### Added joins ###########
LEFT JOIN ps_product_base_names pbn ON pbn.id_product = p.id_product
INNER JOIN (
SELECT base_name, MAX(id_product) AS Max_ID_product
FROM ps_product_base_names
WHERE id_product IN (568110,568129,568134,568135,568136,568137,568139,568140,568141,602911,612411,612413,612512,612513,612515,612612,612616,616213,616217)
GROUP BY base_name) groupedpbn
ON (pbn.base_name = groupedpbn.base_name AND pbn.id_product = groupedpbn.Max_ID_product)
## ########### End added ###########
LEFT JOIN ps_image i ON (i.id_product = p.id_product) LEFT JOIN ps_image_shop image_shop ON (image_shop.id_image = i.id_image AND image_shop.id_shop = 1 AND image_shop.cover=1)
LEFT JOIN ps_image_lang il ON (image_shop.id_image = il.id_image AND il.id_lang = 7)
LEFT JOIN ps_manufacturer m ON (m.id_manufacturer = p.id_manufacturer)
WHERE product_shop.active = 1 AND product_shop.visibility IN ("both", "catalog")
AND c.nleft >= 3 AND c.nright <= 4
AND c.active = 1
AND p.id_product IN (568110,568129,568134,568135,568136,568137,568139,568140,568141,602911,612411,612413,612512,612513,612515,612612,612616,616213,616217)
GROUP BY product_shop.id_product
ORDER BY pl.name asc LIMIT 0,30
I have added 2 JOINs (see comment) to retrieve products by their base name and get only 1 result per base name, to show in the main catalog overview page.
This all works fine, but now I would like to get the number of products that have been grouped per base name. Something like:
COUNT(id_product) AS product_variations
So let's suppose that the products with id_product 568110, 602911 & 612413 all have the same base name, the above query will return id_product 612413 as the result.
But how do I get the number of ID's that have been aggregated (3 for the product with id_product 612413) for every product in the result list?

I found a solution here: https://www.periscopedata.com/blog/use-subqueries-to-count-distinct-50x-faster.html
My working query now looks like this:
SELECT SQL_CALC_FOUND_ROWS
p.*,
product_shop.*,
product_shop.id_category_default,
pl.*,
pbn.*,
cnt.product_variations, ## This line was added for the COUNT specified in the JOIN (see below)
MAX(image_shop.id_image) id_image,
il.legend,
m.name manufacturer_name,
0 as quantity
FROM ps_category_product cp
LEFT JOIN ps_category c ON (c.id_category = cp.id_category)
LEFT JOIN ps_product p ON p.id_product = cp.id_product
INNER JOIN ps_product_shop product_shop ON (product_shop.id_product = p.id_product AND product_shop.id_shop = 1)
LEFT JOIN ps_product_lang pl ON (pl.id_product = p.id_product AND pl.id_shop = 1 AND pl.id_lang = 7)
## ########### Added joins ###########
LEFT JOIN ps_product_base_names pbn ON pbn.id_product = p.id_product
INNER JOIN (
SELECT base_name, MAX(id_product) AS Max_ID_product
FROM ps_product_base_names
WHERE id_product IN (568110,568129,568134,568135,568136,568137,568139,568140,568141,602911,612411,612413,612512,612513,612515,612612,612616,616213,616217)
GROUP BY base_name) groupedpbn
ON (pbn.base_name = groupedpbn.base_name AND pbn.id_product = groupedpbn.Max_ID_product)
## This JOIN was added to COUNT aggregated product IDs
LEFT JOIN (
SELECT base_name, COUNT(id_product) AS product_variations
FROM ps_product_base_names
WHERE id_product IN (568110,568129,568134,568135,568136,568137,568139,568140,568141,602911,612411,612413,612512,612513,612515,612612,612616,616213,616217)
GROUP BY base_name) cnt
ON (pbn.base_name = cnt.base_name)
## ########### End added ###########
LEFT JOIN ps_image i ON (i.id_product = p.id_product) LEFT JOIN ps_image_shop image_shop ON (image_shop.id_image = i.id_image AND image_shop.id_shop = 1 AND image_shop.cover=1)
LEFT JOIN ps_image_lang il ON (image_shop.id_image = il.id_image AND il.id_lang = 7)
LEFT JOIN ps_manufacturer m ON (m.id_manufacturer = p.id_manufacturer)
WHERE product_shop.active = 1 AND product_shop.visibility IN ("both", "catalog")
AND c.nleft >= 3 AND c.nright <= 4
AND c.active = 1
AND p.id_product IN (568110,568129,568134,568135,568136,568137,568139,568140,568141,602911,612411,612413,612512,612513,612515,612612,612616,616213,616217)
GROUP BY product_shop.id_product
ORDER BY pl.name asc LIMIT 0,30
To be honest, it works and it seems fast enough in my test environment, but I haven't got a clue if this solution is efficient or not. So any comments to possibly improve on my solution are still welcome.
Thanks, Mattie

Related

Optimize mysql query using various join and order by rand()

I was having trouble on my site, so I went look into mysql slow query log.
Fixed some indexing and also querys without joins now the server load got lower and its running fine.
I still have a query that I havent been able to fix, if any of you have a tip so I can make it better, I would appreciated it.
This is the query
SELECT p.product_id,
p.product_offer_price,
ps.subcategory_name,
pb.brand_name,
pm.model_name,
pm.display_name,
pm.year_from,
pm.year_to
FROM product p
INNER JOIN product_subcategory ps ON ps.subcategory_id = p.product_subcategory_id AND ps.subcategory_category_id = 12
INNER JOIN product_stock pq ON pq.product_id = p.product_id AND pq.product_quantity > 0
INNER JOIN product_photos pp ON pp.product_id = p.product_id
INNER JOIN product_brand pb ON pb.brand_id = p.product_brand_id
INNER JOIN product_model pm ON pm.model_id = p.product_model_id
GROUP BY p.product_id
ORDER BY RAND() LIMIT 4;
This is what explain shows about the query
I was trying to accomplish something like this, but doesn't work
SELECT p.product_id, p.product_offer_price, ps.subcategory_name, pb.brand_name, pm.model_name, pm.display_name, pm.year_from, pm.year_to
FROM product p
INNER JOIN product_subcategory ps ON ps.subcategory_id = p.product_subcategory_id
AND ps.subcategory_category_id =12
INNER JOIN product_stock pq ON pq.product_id = p.product_id
AND pq.product_quantity >0
INNER JOIN product_photos pp ON pp.product_id = p.product_id
INNER JOIN product_brand pb ON pb.brand_id = p.product_brand_id
INNER JOIN product_model pm ON pm.model_id = p.product_model_id
WHERE p.product_id >= FLOOR( 1 + RAND( ) * (
SELECT MAX( product_id )
FROM product ) )
GROUP BY p.product_id
LIMIT 4

Prestashop order products sql

Where I can find Prestashop SQL query where they display the products that were bought in order.
Like this:
I'm creating a custom page that shows 100 orders in table. and if clicked on order its shows the products that were ordered. I made an sql query:
SELECT product_id, product_reference AS kood,product_name AS nimetus,product_quantity AS nr, pl.link_rewrite,psa.quantity as kogukogus,group_concat(ps.product_supplier_reference) as supp_ref, p.location AS asukoht,
CONCAT(c.link_rewrite,'/',p.id_product,'-',pl.link_rewrite,'.html') link
FROM ps_order_detail o
LEFT JOIN ps_product_lang pl on o.product_id = pl.id_product
LEFT JOIN ps_product p on p.id_product = pl.id_product
LEFT JOIN ps_stock_available psa on p.id_product = psa.id_product
LEFT JOIN ps_category_lang c on c.id_category = p.id_category_default
LEFT JOIN ps_product_supplier ps on p.id_product = ps.id_product
WHERE pl.id_lang=2 AND c.id_lang=2 AND id_order= '".$q."'
GROUP BY product_id, kood,nimetus,nr,pl.link_rewrite, kogukogus,asukoht, link
But when product has attributes it duplicates products.
In this case iButton has two colors black and red(attributes). In this order only one black iButton was bought with quantity of 20, but It shows three rows.
I tried to fix this, but that arised another problems with my sql query, that when it has no attribute it dosen't show anything.
SELECT product_id, product_reference AS kood,product_name AS nimetus,product_quantity AS nr, pl.link_rewrite,psa.quantity as kogukogus,group_concat(ps.product_supplier_reference) as supp_ref, p.location AS asukoht,
CONCAT(c.link_rewrite,'/',p.id_product,'-',pl.link_rewrite,'.html') link
FROM ps_order_detail o
JOIN ps_product_lang pl on o.product_id = pl.id_product
JOIN ps_product p on p.id_product = pl.id_product
JOIN ps_stock_available psa on p.id_product = psa.id_product
JOIN ps_category_lang c on c.id_category = p.id_category_default
JOIN ps_product_supplier ps on p.id_product = ps.id_product
LEFT JOIN ps_product_attribute pa on p.id_product=pa.id_product
WHERE pl.id_lang=2 AND pa.id_product_attribute = psa.id_product_attribute AND pa.id_product_attribute =o.product_attribute_id AND c.id_lang=2 AND id_order= '".$q."'
GROUP BY product_id, kood,nimetus,nr,pl.link_rewrite, kogukogus,asukoht, link
How can I fix this problem?
I think that you have to move 2 of your WHERE conditions to the JOIN conditions. Probably it is enough to move only the condition on the LEFT JOIN.
This should be the result:
SELECT DISTINCT product_id, product_reference AS kood,product_name AS nimetus,product_quantity AS nr, pl.link_rewrite,psa.quantity as kogukogus,group_concat(ps.product_supplier_reference) as supp_ref, p.location AS asukoht,
CONCAT(c.link_rewrite,'/',p.id_product,'-',pl.link_rewrite,'.html') link
FROM ps_order_detail o
JOIN ps_product_lang pl on o.product_id = pl.id_product
JOIN ps_product p on p.id_product = pl.id_product
JOIN ps_stock_available psa on p.id_product = psa.id_product
JOIN ps_category_lang c on c.id_category = p.id_category_default
JOIN ps_product_supplier ps on p.id_product = ps.id_product
LEFT JOIN ps_product_attribute pa on p.id_product=pa.id_product
AND psa.id_product_attribute = pa.id_product_attribute
WHERE pl.id_lang=2 AND c.id_lang=2 AND id_order= '".$q."'
Use SELECT DISTINCT instead simple SELECT in your query if you aren't interesting about particular product attributes for your result
Thanks for your answers, this is really silly but when removing kogukogus from group by and result as this:
SELECT product_id, product_reference AS kood,product_name AS nimetus,product_quantity AS nr, pl.link_rewrite,psa.quantity as kogukogus,group_concat(ps.product_supplier_reference) as supp_ref, p.location AS asukoht,
CONCAT(c.link_rewrite,'/',p.id_product,'-',pl.link_rewrite,'.html') link
FROM ps_order_detail o
LEFT JOIN ps_product_lang pl on o.product_id = pl.id_product
LEFT JOIN ps_product p on p.id_product = pl.id_product
LEFT JOIN ps_stock_available psa on p.id_product = psa.id_product
LEFT JOIN ps_category_lang c on c.id_category = p.id_category_default
LEFT JOIN ps_product_supplier ps on p.id_product = ps.id_product
WHERE pl.id_lang=2 AND c.id_lang=2 AND id_order= '".$q."'
GROUP BY product_id, kood,nimetus,nr,pl.link_rewrite,asukoht, link
I got it working somehow. Thanks you #kiks73, Your code didn't work, BUT when I was trying to add group by to you're code, I realized that the problem was with group by. So the bounty goes to you since no one answered anything serious

Mysql query with multiples conditions

I have to get products in multiples categories (Prestashop database).
This my query :
SELECT COUNT( cp.`id_product` ) AS total
FROM `ps_product` p
INNER JOIN ps_product_shop product_shop ON ( product_shop.id_product = p.id_product
AND product_shop.id_shop =1 )
LEFT JOIN `ps_category_product` cp ON p.`id_product` = cp.`id_product`
WHERE cp.`id_category` =6
AND cp.`id_category` =126
AND product_shop.`visibility`
IN (
"both", "catalog"
)
AND product_shop.`active` =1
I want to select inside category 6 AND inside category 126, but my query return 0.
A product can have multiples categories, so i want to select only if products are inside the two categories.
How can this be fixed, so I am getting the expected result?
SELECT COUNT( cp.id_product ) AS total
FROM ps_product p
Left JOIN ps_product_shop product_shop ON ( product_shop.id_product = p.id_product
AND product_shop.id_shop =1 )
LEFT JOIN ps_category_product cp ON p.id_product = cp.id_product
WHERE cp.id_category in(6,126)
AND product_shop.visibility IN ('both', 'catalog')
AND product_shop.active =1
Friend Try this,
SELECT COUNT( cp.id_product ) AS total
FROM ps_product p
Left JOIN ps_product_shop product_shop ON ( product_shop.id_product = p.id_product
AND product_shop.id_shop =1 )
LEFT JOIN ps_category_product cp ON p.id_product = cp.id_product
and cp.id_category in(6)
LEFT JOIN ps_category_product cp1 ON p.id_product = cp1.id_product
and cp1.id_category in(126)
WHERE cp.id_category is not null
and cp1.id_category is not null
AND product_shop.visibility IN ('both', 'catalog')
AND product_shop.active =1
Use
WHERE cp.`id_category` =6
OR cp.`id_category` =126
Do u mean between 6 and 126? It is not possible to validate against one column for both 6 and 126. If it is between then use,
SELECT COUNT( cp.`id_product` ) AS total
FROM `ps_product` p
INNER JOIN ps_product_shop product_shop ON ( product_shop.id_product = p.id_product
AND product_shop.id_shop =1 )
LEFT JOIN `ps_category_product` cp ON p.`id_product` = cp.`id_product`
WHERE cp.`id_category` between 6 and 126
AND product_shop.`visibility`
IN (
"both", "catalog"
)
AND product_shop.`active` =1
WHERE (cp.`id_category` = 6 OR cp.`id_category` = 126)
AND product_shop.`visibility` IN ("both", "catalog")
AND product_shop.`active` = 1
In both categories:
SELECT COUNT( cp.id_product ) AS total
FROM ps_product p
Left JOIN ps_product_shop product_shop ON (product_shop.id_product = p.id_product
AND product_shop.id_shop = 1)
WHERE product_shop.visibility IN ('both', 'catalog')
AND product_shop.active = 1
AND EXISTS(SELECT * FROM ps_category_product cp
WHERE p.id_product = cp.id_product AND cp.id_category = 6)
AND EXISTS(SELECT * FROM ps_category_product cp
WHERE p.id_product = cp.id_product AND cp.id_category = 12)

Query goes wrong when checking if the user exists in other table

I have a query that shows a list of revisions and employees for every revision..
Now I'm trying to show if the given employee already has a row in the answers table..
This is the overview of the database
This is my working query that shows the list of revisions and employees
SELECT l.id, l.naam, r.id as revision_id, r.beschrijving, e.id as employee_id, e.voornaam, e.achternaam,
FROM lists l
INNER JOIN revisions r ON l.id = r.list_id
INNER JOIN employeelists el ON el.list_id= l.id
INNER JOIN employees e ON e.id = el.employee_id
INNER JOIN customers c ON c.id = e.customer_id
WHERE customer_id = :id AND r.actief = 1
Now I've tried several things to see if the employee already has a record in the answers table.. But It's failing the whole time..
Try 1 : Adding the Answers table with a left outer join
SELECT l.id, l.naam, r.id as revision_id, r.beschrijving, e.id as employee_id, e.voornaam, e.achternaam,
**CASE WHEN a.coach_id != 0 THEN 1 ELSE 0 END as FILLED IN**
FROM lists l"""
INNER JOIN revisions r ON l.id = r.list_id
**LEFT OUTER JOIN answers a ON a.revision_id = r.id**
INNER JOIN employeelists el ON el.list_id= l.id
INNER JOIN employees e ON e.id = el.employee_id
INNER JOIN customers c ON c.id = e.customer_id
WHERE customer_id = :id AND r.actief = 1
now the problem is that every employee is shown multiple times...
This is the SQLFiddle of the working database, The only thing i can't do is check if the given employee ( werknemer ) exists in the answers ( antwoorden ) table..
http://sqlfiddle.com/#!2/0c01c/4
Any idea on how i can solve this? I tried a subquery, but that didn't work out either.. Thanks!
Problem with Query Now
I thought i was correct but there's one more error. in the answers table, it shows results for werknemer_id ( employee_id ) = 78. For the revisie ( revision ) 1 and 2
While there is only results for revisie 1 (screenshot below)
Thanks!
How about this exist column will have 0 if not in antwoorden and 1 if exist
SELECT l.id, l.naam, r.revisie as revisie, r.id as revisie_id, r.beschrijving, w.id as werknemer, w.voornaam, w.achternaam
, a.werknemer_id,
(CASE WHEN a.werknemer_id IS NULL THEN 0 ELSE 1 END ) AS `exist`
FROM lijsten l
INNER JOIN revisies r ON l.id = r.lijst_id
INNER JOIN werknemerlijsten wl ON wl.lijst_id = l.id
INNER JOIN werknemers w ON w.id = wl.werknemer_id
INNER JOIN klanten k ON k.id = w.klant_id
LEFT JOIN antwoorden a ON w.id = a.werknemer_id
WHERE klant_id = 39 AND r.actief = 1
GROUP BY r.beschrijving, w.id
Fiddle
Final solution
This is the final outcome, only see 'ingevuld' is 1 if the desired coach (1 in thise case) is in the answers table.
SELECT l.id, l.naam, r.revisie AS revisie, r.id AS revisie_id, r.beschrijving, w.id AS werknemer, w.voornaam, w.achternaam, a.coach_id,
CASE WHEN a.coach_id = 1 THEN 1 ELSE 0 END AS ingevuld
FROM lijsten l
INNER JOIN revisies r ON l.id = r.lijst_id
INNER JOIN werknemerlijsten wl ON wl.lijst_id = l.id
INNER JOIN werknemers w ON w.id = wl.werknemer_id
INNER JOIN klanten k ON k.id = w.klant_id
LEFT JOIN antwoorden a ON w.id = a.werknemer_id AND r.id=a.revisie_id
WHERE klant_id = 39 AND r.actief = 1
group by r.id, w.id, a.coach_id

How to access outer table in a LEFT OUTER JOIN

In the query below, I am trying to use the first table in a left outer join. However I am getting an error.
SELECT
products.id,
products_cstm.oem_c,
products.mfr_part_num,
products.description,
products.cost,
products.assigned_user_id,
customfields_oo.ans
FROM products
LEFT OUTER JOIN (SELECT COUNT( q.id ) AS ans
, pq.product_id
FROM products_quotes pq
LEFT JOIN quotes q
ON pq.quote_id = q.id
WHERE q.deleted = 0
AND pq.deleted = 0
AND q.stage <> 4
AND (pq.qty_shipped < pq.product_qty)
AND pq.product_id = products.id
GROUP BY pq.product_id
) AS customfields_oo
ON customfields_oo.product_id = products.id
LEFT JOIN products_cstm
ON products.id = products_cstm.id_c
WHERE products.deleted = 0
ORDER BY ans DESC
When I run the query it gives me the following error:
Error Code : 1054
Unknown column 'products.id' in 'where clause'
It is not allowing first "products" table in left outer join query.
The issue is that customfields_oo is a derived table not a correlated subquery. Thus, you cannot reference the outer table from within the definition of the derived table. In this case, you cannot refer to the outer products table from within the customfields_oo definition. Instead, you must do that filter in the On clause outside the dervied table definition.
Select products.id,
products_cstm.oem_c,
products.mfr_part_num,
products.description,
products.cost,
products.assigned_user_id,
customfields_oo.ans
FROM products
Left Join (
Select pq1.product_id
, Count( q1.id ) As ans
From products_quotes As pq1
Left Join quotes As q1
On pq1.quote_id = q1.id
Where q1.deleted = 0
And pq1.deleted = 0
And q1.stage <> 4
And pq1.qty_shipped < pq1.product_qty
Group By pq1.product_id
) As customfields_oo
On customfields_oo.product_id = products.id
Left Join products_cstm
On products.id = products_cstm.id_c
Where products.deleted = 0
Order By customfields_oo.ans Desc
Now, you have stated in comments that this is too slow because, say products where deleted <> 0 might be evaluated in the derived table. If that is the case, then simply expand the derived table to include the filters on the outer products table.
Select products.id,
products_cstm.oem_c,
products.mfr_part_num,
products.description,
products.cost,
products.assigned_user_id,
customfields_oo.ans
FROM products
Left Join (
Select pq1.product_id
, Count( q1.id ) As ans
From products_quotes As pq1
Join products As p1
On p1.products.id = pq1.product_id
Left Join quotes As q1
On pq1.quote_id = q1.id
Where q1.deleted = 0
And pq1.deleted = 0
And q1.stage <> 4
And pq1.qty_shipped < pq1.product_qty
And p1.deleted = 0
Group By pq1.product_id
) As customfields_oo
On customfields_oo.product_id = products.id
Left Join products_cstm
On products.id = products_cstm.id_c
Where products.deleted = 0
Order By customfields_oo.ans Desc
You do not need to have AND pq.product_id = products.id in the where statement. Because you are LEFT JOINing on that. So I think something like this will work:
AND (pq.qty_shipped < pq.product_qty)
GROUP BY pq.product_id) AS customfields_oo
ON customfields_oo.product_id = products.id
LEFT JOIN products_cstm
ON products.id = products_cstm.id_c
WHERE products.deleted = 0
ORDER BY openorder DESC
EDIT
You do not need to LEFT JOIN on the table you are COUNTing on. You can also do ot like this:
SELECT
.....
(
SELECT
COUNT( q.id )
FROM products_quotes pq
LEFT JOIN quotes q
ON pq.quote_id = q.id
WHERE q.deleted = 0
AND pq.deleted = 0
AND q.stage <> 4
AND (pq.qty_shipped < pq.product_qty)
AND pq.product_id = products.id
) AS ans
FROM products
.....