Price search on multiple variant row using sql - mysql

I have a below price table
required each product have a multiple prices in single row
my sql query is
select a.out_of_stack, product.*,
( select min(price) from temp_attr
where product_id =product.product_id
) as min_price ,
( select max(price) from temp_attr
where product_id = product.product_id
) as max_price from product
left join users a
on a.user_id = product.user_id
where product.category_id in (37903,4707)
and product.product_status in ('0')
and product.draft=0
and a.active=0
and product.product_close=0
and product.price between 10 and 4000
group by product.product_id
i need to get the product when search using price with temp_attr table.

Find Max/Min prices for products in temp_attr :
SELECT MIN(price) , MAX(price) from temp_attr group by product_id
Join them with product and user tables:
SELECT a.out_of_stack , product.*, t1.* FROM
product
left join (SELECT MIN(price) , MAX(price) from temp_attr group by product_id ) as t1
on t1.product_id = product.product_id
left join users as a
on a.user_id = product.user_id
where product.category_id in (37903,4707)
and product.product_status in ('0')
and product.draft=0
and a.active=0
and product.product_close=0
and product.price between 10 and 4000
group by product.product_id

Related

i want to get lowest price values with id in mysql

my query is
SELECT productscrapeddatalog.*, product.productname
FROM productscrapeddatalog JOIN product
ON productscrapeddatalog.productID = product.productID
WHERE price = (SELECT MIN(price) FROM productscrapeddatalog ORDER BY productID)
this is my table screenshot
Your approach with a join and filtering in the where clause is ok - but you need to correlate the subquery with the outer query so you get the lowest price per product rathern thatn the overall min:
SELECT l.*, p.productname
FROM productscrapeddatalog l
JOIN product p ON l.productID = l.productID
WHERE l.price = (
SELECT MIN(l1.price)
FROM productscrapeddatalog l1
WHERE l1.productID = t.productID
)
If you are running MySQL 8.0, you can also do this with window functions:
SELECT l.*, p.productname
FROM product p
JOIN (
SELECT l.*, RANK() OVER(PARTITION BY productID ORDER BY price) rn
FROM productscrapeddatalog l
) p ON l.productID = l.productID AND l.rn = 1
You should try this:
SELECT productscrapeddatalog.*, product.productname, MIN(productscrapeddatalog.price)
FROM productscrapeddatalog JOIN product
ON productscrapeddatalog.productID = product.productID
ORDER BY productID

Mysql query for sum two table data by product wise

If I use the below code the sum of sale and purchase product wise perfect but 5 NO product missing because no purchase only sale but I want to show all product. what-ever purchase or sale are zero.
SELECT *
FROM (
SELECT product_id, SUM(quantity) AS sale
FROM order_item group by product_id
) P
JOIN (
SELECT product_id, SUM(quantity) AS purchase
FROM pur_item
group by product_id
) S
JOIN (
SELECT product_id as Pid
FROM product GROUP BY Pid
) I ON I.Pid = P.product_id AND S.product_id = P.product_id
If is use below code then result is like below. I don't know sum of sale and purchase is not perfect.
select p.product_id, sum(s.quantity) sale, sum(c.quantity) purchase
from product p
left join pur_item c on c.product_id = p.product_id
left join order_item s on s.product_id = p.product_id
where c.quantity is not null or s.quantity is not null
group by p.product_id
I want result for all item sum of product wise data what ever sale or purchase made.
You are taking the correct approach in your first query but your JOIN conditions are wrong and you need to use LEFT JOINs instead of JOIN to get products which have no purchases or no sales:
SELECT I.product_id,
COALESCE(S.sale, 0) AS sale,
COALESCE(P.purchase, 0) AS purchase
FROM Product I
LEFT JOIN (
SELECT product_id, SUM(quantity) AS sale
FROM order_item
GROUP BY product_id
) S ON S.product_id = I.product_id
LEFT JOIN (
SELECT product_id, SUM(quantity) AS purchase
FROM pur_item
GROUP BY product_id
) P ON P.product_id = I.product_id
We also use COALESCE to convert NULL values (when a product has no purchases or sales) to 0.

select sum qty from 2 tables

In MySQL I have 4 tables:
- product(id)
- order(id)
- order_detail_1(id, product_id, order_id, qty)
- order_detail_2(id, product_id, order_id, qty)
I want to get the sum of the quantity of products sold from the 2 tables (order_detail_1, order_detail_2) grouping them by product
produt can existe in order_detail_1 and not in order_detail_2 and vice versa
i tested this query and it worked but I want a simpler query without the union and the subquery.
select tmp.product_id ,sum(tmp.qty) from
(
(
select order_detail_1.product_id ,sum(order_detail_1.qty)
from order_detail_1
inner join order on order_detail_1.id_order = order.id
where order_detail_1.product_id is not null
group by order_detail_1.product_id
)
union all
(
select order_detail_2.product_id ,sum(order_detail_2.qty)
from order_detail_2
inner join order on order_detail_2.id_order = order.id
where order_detail_2.product_id is not null
group by order_detail_2.product_id
)
) tmp
group by tmp.product_id
It looks like you're not using order table other then checking if it exists, so you can use EXISTS()
SELECT p.product_id,sum(p.qty) as qty
FROM (SELECT product_id,qty,id_order FROM order_detail_1
WHERE product_id IS NOT NULL
UNION ALL
SELECT product_id,qty,id_order FROM order_detail_2
WHERE product_id IS NOT NULL) p
WHERE EXISTS(SELECT 1 FROM order o
WHERE o.id = p.id_order)
GROUP BY p.product_id
If a product is in only one table, you can use left join:
select p.id, (coalesce(sum(od1.qty), 0) + coalesce(sum(od2.qty, 0))) as qty
from product p left join
order_detail_1 od1
on od1.product_id = p.id left join
order_detail_2 od2
on od2.product_id = p.id
group by p.id;
This formulation depends on the fact that the two tables are exclusion -- a product is in only one table.
EDIT:
If products can exist in both tables, then you need to aggregate them first:
select p.id, (coalesce(od1.qty, 0) + coalesce(od2.qty, 0)) as qty
from product p left join
(select product_id, sum(qty) as qty
from order_detail_1 od1
group by product_id
) od1
on od1.product_id = p.id left join
(select product_id, sum(qty) as qty
from order_detail_2 od2
group by product_id
) od2
on od2.product_id = p.id;

sql statement with min and max

I need to write a statement using min and max together.
SELECT companyname FROM companies JOIN stocklist
USING (companyid) where price =some(
( select max(price) from stocklist) , ( select min(price) from stocklist) ) ;
I need to get the companies name for the most low and max value but this isn't working (because of the last line) what is wrong and how should I do that?
UPDATE:
select companyname from stockList join companies using (companyid) WHERE price IN (select min(price) from stocklist) ;
this is working for me but gets only one operand, how can I get two?
I don't like the idea of having two subqueries in an IN clause. Here is another way with the join being made explicit:
SELECT companyname
FROM companies c JOIN
stocklist s
c.companyid = s.companyid join
(select min(price) as minprice, max(price) as maxprice
from stocklist
) sm
on s.price = minprice or s.price = maxprice
Try this:
SELECT companyname FROM companies c
INNER JOIN stocklist s
ON c.companyid = s.companyid
WHERE price IN
((SELECT MAX(price) FROM stocklist ),
(SELECT MIN(price) FROM stocklist ))
SEE THIS FIDDLE

sql statement where there is top5 and inner join

How do i write a sql statement where there is top5 and inner join?
Currently, this is what i have:
$query = "SELECT OrderItem.ProductCode , Product.ProductName,
count(OrderItem.OrderID) as total_orders
FROM `OrderItem`
GROUP BY ProductCode
ORDER BY total_orders DESC LIMIT 5
INNER JOIN Product ON Product.ProductCode = OrderItem.ProductCode";
but it's not working.
tables:
Product table has columns:
ProductCode, ProductName, Cat, Qty, CostPr, RetailPr, VendorID
OrderItem table has columns:
OrderID, ProductCode, UnitPr, Qty,TotalPr
My objective is to display the productcode and productname of the top 5 products in orders submitted. Please help. thanks.
A JOIN is part of the FROM clause, and should come before your GROUP BY:
SELECT
OrderItem.ProductCode,
Product.ProductName,
count(OrderItem.OrderID) as total_orders
FROM
OrderItem
INNER JOIN Product
ON Product.ProductCode = OrderItem.ProductCode
GROUP BY OrderItem.ProductCode, Product.ProductName
ORDER BY total_orders DESC LIMIT 5
select ol.ProductCode, ol.total_orders, p.ProductName
from (
select ProductCode, count(OrderID) as total_orders
from OrderItem
group by ProductCode
order by total_orders desc LIMIT 5
) ol
inner join Product p on p.ProductCode = ol.ProductCode