How should i order by price in this case - php

I want to order by price. But I am getting the price from two different table and compare it to get the minimum price and print it if price is above 0. It is also possible that price is present in only one database. The rough structure of my code is:
query1 = choose products
while = fetch the products details {
query2 = get the price1, order by price1
query3 = get the price2, order by price2
if statement to get the minimum price so price = minimum price
if (price>0) {
echo product result
}
}
How can I order the result according to the price?
EDIT: Product is already selected, i want to acquire the price of products from two different price table

You can do it in just one query, much more efficiently:
SELECT `a`.*, LEAST(`b`.`price`,`c`.`price`) AS `price`
FROM `products` `a`
JOIN `price1` `b` ON `a`.`id`=`b`.`product_id`
JOIN `price2` `c` on `a`.`id`=`c`.`product_id`
WHERE `price`>0
ORDER BY `price` ASC
Adjust according to your database's tables and columns.

Related

Update Subtract qty - SQL

I have the following DB structure
Table product (which holds my products information including the stock)
prod_id
prod_name
prod_desc
prod_price
stock
table store_orders_item (which is updated every time a customer buys from the my site)
id
order_id
sel_prod_id
sel_prod_price
sel_prod_qty
I need that every time I make a sale online and store_orders_items is updates, that reduce the stock field of the product sold on the table product
I think i need to use the code below, plus some sort of join or subquery to update my product table according to the prod_id, but i could not figure out how.
UPDATE product SET stock = stock - (SELECT SUM(sel_prod_qty) FROM store_orders_items);
I also tried:
UPDATE product
SET stock = stock - (SELECT s.sel_prod_qty, s.sel_prod_id, p.prod_id
FROM store_orders_items as s left join product as p ON
s.sel_prod_id=p.prod_id WHERE order_id=(SELECT MAX(order_id) from orders_number));
And got this error:
"You can't specify target table 'product' for update in FROM clause"
You must use join updates, for example in MySQL:
UPDATE
product p
INNER JOIN
store_orders_item s
ON
p.prod_id = s.sel_prod_id
SET
p.stock = p.stock - s.sel_prod_qty
WHERE
s.order_id = NNNNN
where NNNNN is the id of order just inserted.

mysql query to get count of products along with quantity

SELECT product,quantity,count(product) AS count
FROM order_table WHERE order_id in
(select order_id from progress)
group by product
using this query i get product name and count of products. but, i also have quantity column in my table. so, when product gets 2 quantity how to add that quantity along with product count with this above query.
in the above image there are 3 burger products and 2 quantity for one burger product, here i need to show like product : burger and total quantity : 4 later in next line product : pizza and total quantity : 3
SELECT product,sum(quantity)as 'quantity',count(product) AS count
FROM order_table WHERE order_id in
(select order_id from progress)
group by product
Just a use aggrigate function sum on quantity.
Need some sample data. Input data and expected output.
Till then try it out this query --
SELECT Product
,SUM(Quantity) AS Total_Quantity
,COUNT(Product) AS Product_Count
FROM order_table
WHERE order_id IN (
SELECT DISTINCT order_id
FROM Progress
)
GROUP BY Product
Without sample data it is difficult to guess, but looking at your question, looks like you need something like this
SELECT product
,sum(quantity) as total_quantity
,count(*) AS count
FROM order_table
WHERE order_id in
(select order_id from progress)
group by product

php shopping cart -group by selection and get the total

My question is a little bit long...
This is the is the first picture for my question:
---Here it is the query how I make the selection:
$costumer_name=$_SESSION['p_user'];
$date=date("y-m-d");
$query_8="select * from orders
where costumer_name='$costumer_name' && date='$date'
order by product_id desc";
$result_8=mysql_query($query_8);
---As you see, the selection table needs a group by query and here is that query and the result:
$costumer_name=$_SESSION['p_user'];
$date=date("y-m-d");
$query_8="select * from orders
where costumer_name='$costumer_name' && date='$date'
group by product_id desc";
$result_8=mysql_query($query_8);
---Here it is how I get the SUM:
$query_9="select SUM(totali) as totali from orders where costumer_name='$costumer_name' && date='$date'";
$result_9=mysql_query($query_9);
$row_9=mysql_fetch_array($result_9);
$totali=$row_9['totali'];
---And finaly the problem:
1.How to count the number of orders with the same product_id(sasia)?
2.Get the first total of -cmimi and -sasia?
3.Get the final total?
Thank you
first of all i think there is better solution then every time when somebody click add to cart button to always insert new row in the database. If product is already there you should update quantity by +1, that way you will have only one row of one products instead of 3. I don't know exactly how your database look but here is an idea how you could do that:
CREATE PROCEDURE add_to_cart(IN in_cart_id INT, IN in_product_id...)
BEGIN
DECLARE quant INT;
SELECT quantity
FROM cart
WHERE cart_id = in_cart_id
AND product_id = in_product_id
INTO quant;
IF quant IS NULL then
INSERT INTO cart....
ELSE
UPDATE cart
SET quantity = quantity + 1
WHERE cart_id = in_cart_id
AND product_id = in_product_id;
END IF;
END$$
I think that is much better solution but if you want to do that the way you started then here is a way to do that:
SELECT productId, name, price, COUNT(quantity) as quantity, (price * COUNT(quantity)) as total
FROM where-ever-you store data
WHERE expression
GROUP BY productId;
that is answer on two your question, for the third total sum you need to write separate SELECT statement
SELECT SUM(price * quantity) as total_sum
FROM where-ever-you-store-data
WHERE expression if needed...

How could/should I write this SQL query?

I need to write an SQL query to list a customers prices for our products. We have a standard price list, customer_no = 0, and a customer specific price list, customer_no = XXXX.
I am having trouble understanding how I can get the query to return the customer specific price for a product, if they've been given such, or if not fall back to the standard price.
To get all products and prices on the standard price list
select prices.product_id, products.product_desc, prices.m2
from prices, products
where prices.product_id = products.product_id
and prices.customer_no = 0
order by prices.product_id asc
To get all products and prices that the customer has been specifically quoted for
select prices.product_id, products.product_desc, prices.m2
from prices, products
where prices.product_id = products.product_id
and prices.customer_no = $_SESSION['customer']
order by prices.product_id asc
How can I perform the first query, but if the customer has their own price then replace it with that? Is this even possible?
Thanks in advance.
Steve
Edit: Sorry, missed the third line in both queries in the original post.
You have to join with the prices table twice, once for the list prices and then for the quoted prices. Use a LEFT JOIN for the latter, since some products won't have a quoted price. Then use NVL to default from the quoted price to the list price.
SELECT products.product_id, products.product_desc, NVL(p2.m2, p1.m2)
FROM products
JOIN prices p1 ON p1.product_id = products.product_id
LEFT JOIN prices p2 ON p2.product_id = products.product_id
AND p2.customer_no = $_SESSION['customer']
WHERE p1.customer_no = 0
Try:
select prices.product_id, products.product_desc, prices.m2
from prices, products
where prices.customer_no = nvl($_SESSION['customer'], 0)
order by prices.product_id asc

updating sql values with array which have a couple times occured items

While i am coding a shopping site, I need to update product stock.
But the thing is, naturally shopping cart can have the same items a couple of times.
What is the best way for updating it?
I tried IN but the following SQL query returns 3 items.
SELECT *
FROM `products`
WHERE id
IN ( 3, 4, 4, 6 )
LIMIT 0 , 30
Here is my solution but, I don't think this is the best one.
$cart = array(1,3,4,4,5,8,22,22);
$itemlist = array_count_values($cart);
foreach($itemlist as $itemid=>$ocurrence){
$SQL = "UPDATE products SET stock = stock-".$ocurrence." WHERE id = ".$itemid;
mysql_query($SQL);
}
You can do something like this:
SELECT * FROM menu WHERE item_id = 1
UNION ALL
SELECT * FROM menu WHERE item_id = 1
UNION ALL
SELECT * FROM menu WHERE item_id = 2
Check this link:
MySQL table -> Can you return the same row multiple times, in the same query?
if possible create a seperate item_cart, table which will have (cart_id, item_id , product_id) as primary key. from here you can get do a group by on product_id to see how many no of product sold.
select product_id, count(product_id) as "No of Product Sold" from item_cart
group by product_id
your php code will update no of products in stock coloumn perfectly.
If possible you try setting triggers for updatin stock columns whenever any product is sold.

Categories