i have been trying now 2 days and doing research about this but i din't find any solution for this
so i have 3 tables what i want is that if a product is having only single price then show that single price if the product is having multiple prices then show the min price and max price of that product.
so in product id number 2 is having 3 prices.
so if the product is having single price then each product will have their price
if product has multiple prices then display min price $ 19.00 __ $ 26.00 max price.
so how can i do this?? how can i display it on the page???
product (id, price)
1 1
2 2
3 3
product_has_price (product_id, price_id)
1 1
2 2
2 3
3 4
2 5
price (id, price)
1 14.00
2 19.00
3 24.00
4 35.00
5 26.00
thanks if anybody helps
select p.id
, case
when min(i.price) <> max(i.price) then
concat('$ ', min(i.price), '__ $ ', max(i.price))
else concat('$ ', min(i.price))
end as PriceDescription
from product p
join product_has_price php
on p.id = php.product_id
join price i
on i.id = php.price_id
group by
p.id
Related
Basically I have two tables itemTable and stockTable, every time I add a stock, I will get the foreign key for itemId and add it in stockTable with quantity.
I want to update the stockquantity in stockTable every time the customer will buy a product.
PROBLEM:
For example the customer will buy 7 shirt:
SO FIRST WILL UPDATE ON THE STOCKID (3) QUANTITY OF 4
SECOND WILL UPDATE THE STOCKID (6) QUANTITY OF 3
SO THE TOTAL STOCKS OF SHIRT IS 3 AFTER UPDATING THE QUANTITY
THE STOCKID (3) WILL BECOME 0
THE STOCKID (6) WILL BECOME 3
HOW DO I QUERY THAT IN SQL.
THANK YOU!
TOTAL STOCKS: (Base on the same itemName)
SHOES - 5
SHIRT - 10
BALL - 15
stockTable
stockId itemId stockQuantity stockOut
1 35 5
3 89 4
6 11 6
14 87 15
itemTable
itemId itemName itemSellPrice supplierName
35 shoes 50 Jason
89 shirt 40 Jacob
11 shirt 65 Max
87 ball 150 Sam
First of all you need to identify the itemId(s) that must be updated, they are in the result of:
SELECT itemId FROM itemTable WHERE itemName = 'shirt'
Having said that the update query will look like this:
UPDATE stockTable SET ... WHERE itemId IN (SELECT itemId FROM itemTable WHERE itemName = 'shirt')
Obviously now we need to update the quantities, this must be done progressively until the order is complete starting from the lowerst itemId (for istance):
SELECT IF(stockQuantity>7,#remaining_qty:=stockQuantity-7,#remaining_qty:=#remaining_qty-stockQuantity) AS `remaining_qty` FROM stockTable WHERE itemId IN (SELECT MIN(itemId) FROM itemTable WHERE itemName = 'shirt';
UPDATE stockTable SET stockQuantity=IF(stockQuantity>7,stockQuantity-7,0) WHERE itemId IN (SELECT MIN(itemId) FROM itemTable WHERE itemName = 'shirt');
After that you can check the variable #remaining_qty, and reapeat the stuff until #remaining_qty>0.
Q : I am trouble to do following task in mysql query.
Task is get all products (If product is duplicate than sum of it's qty) and deduct wastage stock (If wastage product is duplicate than sum of it's qty).
I have two tables like,
1) manage_stock
2) manage_wastage
manage_stock
=> This table has p_id(Product ID) and many rows with product duplication's.
p_id p_name p_qty
1 Pro-1 10
2 Pro-2 15
3 Pro-3 8
1 Pro-1 15
manage_wastage
=> This table has p_id(Product ID) of manage_stock table. It is also many rows with product duplication's.
p_id w_qty
1 2
1 4
3 5
Desired Output
p_id p_name p_qty w_qty final_qty
1 Pro-1 20 6 14
2 Pro-2 15 0 15
3 Pro-3 8 5 3
Thank you very much.
You just have to compute the difference between the stock quantity and the wastage
SELECT s.p_id, s.p_name, SUM(p_qty), SUM(w_qty), SUM(p_qty) - SUM(w_qty) as final_qty
FROM manage_stock s
LEFT OUTER JOIN manage_wastage w
ON s.p_id = w.p_id
GROUP BY s.p_id, s.p_name
try this one
SELECT s.p_id, s.p_name, SUM(p_qty),SUM(ifnull(w_qty, 0)),SUM(p_qty - ifnull(w_qty, 0)) as total
FROM manage_stock s
left outer JOIN manage_wastage w
ON s.p_id = w.p_id
GROUP BY s.p_name
its work
SELECT ms.p_id ,GROUP_CONCAT(ms.p_name)p_name ,SUM(ms.p_qty) p_qty ,SUM(mw.w_qty) w_qty,SUM(ms.p_qty)-SUM(mw.w_qty) final_qty FROM manage_stock ms
INNER JOIN manage_wastage mw on ms.p_id =mw.p_id
GROUP BY ms.p_id
Try above code.
As p_name always unique with p_id GROUP_CONCAT() only returns single name.
I have two table
location:
location_id
address
itransfile:
id
transactionNumber
location_id
itemName
quantity
I want to get maximum and minimum sold items by locations.
HighestItemName HighQauntity LowestItemName LowQuantity LocationName
Chicken Burger 50 Tako 5 Gulshan
Chicken Burger 100 Tikka 10 Nipa
Pasta 150 Cheese Burger 12 Liyari
Pizza 200 Chicken Burger 3 F.B.Area
The query I've done so far:
SELECT t.itemName as HighestItemName, sum(t.quantity) as HighQuantity, l.address LocationName
from itransfile as t join locations as l
on t.location_id = l.location_id
where t.location_id IN(1,2,3,4)
group by t.location_id
I don't know how will get max and min items from every group.
Sample Data:
ID TransNumber ItemName Quantity location_id
1 1234 Chicken Burger 3 1
2 1234 Cheese Burger 1 1
3 1235 Sandwich 4 2
4 1332 Salad 1 4
5 14537 Tikka 1 3
6 1236 Roll 3 2
7 1333 Biryani 2 4
location_id address
1 Gulshan
2 Nipa
3 Liyari
4 F.B.Area
This is what you may be looking for if you need it in one query (SQLFiddle):
select
l.address,
imax.itemName max_item, max_min.max_q,
imin.itemName min_item, max_min.min_q
FROM
(select
i.location_id, MAX(i.quantity) max_q, MIN(i.quantity) min_q
FROM
itransfile i
GROUP BY
i.location_id) as max_min
LEFT JOIN itransfile imax ON (max_min.max_q = imax.quantity)
LEFT JOIN itransfile imin ON (max_min.min_q = imin.quantity)
LEFT JOIN location l ON (max_min.location_id = l.location_id)
GROUP BY
l.location_id
It looks for min/max values and then looks up the item name and location address. The GROUP_CONCAT makes sure that when there are more items with the same min/max quantity, you get all of them.
Alternatively you can get rid of the GROUP BY and GROUP_CONCAT and get all the items in rows if you need to further process them.
I have table like below and i'm trying to write single query for fetch the date range data as well as sum of price. My table definitions are as below
Sale table
id_sale date time
1 2014-05-05 12.30 am
2 2014-05-06 10.30 am
3 2014-05-25 12.30 am
Sale Product table
id_sale_product id_sale price quantity id_product
1 1 10.00 1 1
2 1 20.00 1 2
3 2 20.00 3 5
4 3 20.00 4 6
I want to filter by date in sale table and get the sum of the price * quantity = total for every date ie,2014-05-05,2014-05-06..etc
I have tried below query
$query = 'SELECT sp.`id_product_type`,sp.`id_product`,sp.`quantity`,sp.`price`,s.`date`
, SUM(IF(s.`date` BETWEEN "'.$datepickerFrom.'" AND "'.$datepickerTo.'",sp.`price` * sp.`quantity`,0)) AS A
FROM `'._DB_PREFIX_.'sale_product` sp
LEFT JOIN '._DB_PREFIX_.'sales s ON (sp.id_sale = s.id_sale)
WHERE (s.`date` BETWEEN "'.$datepickerFrom.'" AND "'.$datepickerTo.'")';
The output should be :
Example: Date
01/01/2013 Rs.10000
02/01/2013 Rs.140000
03/01/2013 Rs.8000
Example: Month
January Rs. 2,00,000
Feburary Rs. 2,20,000
Try This ....
select a.date,sum(b.qty * b.price) As sum from sales As a, product As b where DATE_FORMAT(a.date,'%d-%m-%Y') between '27-05-2014' AND '28-05-2014' and a.id_sale=b.id_sale group by a.date
My Product table looks like
ID Type Product Code Product Name Price
1 all customers 56620000 Product A 219.68 €
2 all customers 56621000 Product B 4,351.91 €
3 all customers 56622000 Product C 110.98 €
4 155000 56622000 Product C 100.00 €
5 all customers 56626000 Product D 2,410.38 €
6 all customers 56772000 Product E 100.00 €
7 160000 56772000 Product E 90.00 €
If you notice row 3,4 and 6,7 has same product code but with different type and price. That means a product can have a price for all customer as well as for few specific customers. If a customer with customer id 155000 performs a search with product code 56622000, that customer will get price 100.00 € (see row number 4) not 110.98 € (see row number 3). But if that same customer performs a search with product code 56772000, he/she will get price 100.00 €(see row number 6) not 90.00 €(see row number 7). Because there is no specific price for that user for product code 56772000.
My Query: How to execute this operation using PHP and MySql from a single table.?
SELECT *
FROM product
WHERE ProductCode = 'x' AND
Type IN ('y', 'All Customers')
ORDER BY FIELD(Type, 'y', 'All Customers')
LIMIT 1
SQLFiddle Demo
Replace x and y with your desired value shown in the demo.
SELECT *
FROM Products
WHERE ProductCode = 'product_id' AND Type IN ('customer_id', 'All Customers')
ORDER BY Type
LIMIT 1
SELECT * FROM
(SELECT * FROM product
WHERE `Type` = '155000' and ProductCode = '56622000'
LIMIT 1
UNION
SELECT * FROM product
WHERE `Type` = 'all customers' and ProductCode = '56622000'
LIMIT 1) a
LIMIT 1;
SQL FIDDLE DEMO
Thanks for #491243 for the fiddle code.