I have two columns in my database table products.
price discountpercent
100 10
200 70
My Query
SELECT * FROM products order by ?
How can i order by discounted price ?
Use:
ORDER BY price * (100 - discountpercent) / 100
Demo here
Related
We know that magento2 has multiple prices type , for example special price, tier prices, group prices , if one prodcut has multiple price , the magento2 will show the minimum price .
Scenario 1:
Regular Price = 250
Special Price = 200
Group Price (NOT LOGGED) = 180
*** Result in product page: 180
Scenario 2:
Regular Price = 250
Special Price = 120
Group Price (NOT LOGGED) = 180
*** Result in product page: 120
So my question is how to ignore the minimum price , i want to set the group price highest priority , like below sample , i want to show price is 180
Regular Price = 250
Special Price = 120
Group Price (NOT LOGGED) = 180
i have price column in product table from that column i want get price between like 0-5000 and 5001-1000.is it possible in MySQL?
4999.00
9990.00
10990.00
12990.00
11499.00
17000.00
10990.00
12999.00
14200.00
19499.00
20999.00
Using between:
SELECT *
FROM tab_name
WHERE price BETWEEN 0 and 5000;
-- price BETWEEN 5001 AND 10000
I have following table "Cart" & contains the following columns in mysql:
CartID, ProductID, ProductPrice, ProductTax (%), ProductQty
ProductTax is in Percentage but value stored as number/int.
How do i calculate total tax of all products in the cart.
So each product must use its own "ProductTax" Rate ie. in %tage .
(1) example contain 1 product in cart.
ProductPrice: 200 & ProductTax 18%. & ProductQTY:1
So "totaltax" here is: 36.
(2) example contains 2 products in cart
1- ProductPrice: 300 & ProductTax 18% & ProductQTY: 2
2- ProductPrice: 400 & ProductTax 20% & ProductQTY: 1
So for above example "totaltax" is : 108+80= 188
To calculate total tax of all products in the cart:
SELECT SUM((ProductPrice * ProductQty)* ProductTax/100) AS value_sum
FROM Cart
This solves it! – Webgen 10 secs ago edit
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.
I need to find a better way to get the discount for each article price in our web shop depending on which pricelist or pricelist/discount list a customer has. I think this is possible to do in just one query instead of the 5 I have today, but I really do not know where to start.
All our customers have a pricelist and some have both pricelist and one extra discount list. Today we have about 25 different pricelists and about 100 extra discount lists.
All the pricelists are structured in the same manner; they have a price group and a discount in percent.
For example pricelist 01 could look like
A 20
B 35
C 20
The extra discount list is structured in a different manner and can have a fixed price or percentage. It also has three different priority levels: discount based on the article code (has priority 1), based on category (has priority 2) and based on price group (has priority 3).
Discount list 0013 could look like:
In the article tab
PL-2344-444 40 (%)
P-0455-23 200 (SEK)
In the category tab
C12 50 (%)
N12 35 (%)
Today I have three different queries to see if I get a hit in the discount list:
First I check to see if I get a hit in priority 1: (FIXED returns f and PERCENTAGE r)
SELECT DISCOUNT, FIXED, PERCENTAGE FROM PUPRIREG
WHERE ARTCODE = 'JO-23455' AND DISCOUNTLIST = '0013'
If the above returns 0, I do the second query, priority 2:
SELECT DISCOUNT, FIXED, PERCENTAGE FROM PUPRIREG
WHERE CATEGORY = 'C15' AND DISCOUNTLIST = '0013'
And the last one priority 3:
SELECT DISCOUNT, FIXED, PERCENTAGE FROM PUPRIREG
WHERE PRICEGROUP = 'F' AND DISCOUNTLIST = '0013'
If none of the extra discount lists returns 0 I get the discount from the pricelist
SELECT DISCOUNT FROM PUPRIREG WHERE PRICELIST = '01' AND PRICEGROUP = 'F'
I call the function like follows
$discount = discount($articlecode, $category, $pricegroup);
function discount($articlecode, $category, $pricegroup){
$articlecode = sanitizingData($articlecode);
$category = sanitizingData($category);
$pricegroup = sanitizingData($pricegroup);
// do priority 1
// prio 2
// prio 3
// pricelist
return $discount;
}
I would be so happy if someone could show me how to do this. I am using mysqli and php.
Many thanks
Best regards linda
You can do the queries with a union:
(SELECT DISCOUNT, FIXED, PERCENTAGE, 1 priority FROM PUPRIREG
WHERE ARTCODE = 'JO-23455' AND DISCOUNTLIST = '0013')
union
(SELECT DISCOUNT, FIXED, PERCENTAGE, 2 priority FROM PUPRIREG
WHERE CATEGORY = 'C15' AND DISCOUNTLIST = '0013')
union
(SELECT DISCOUNT, FIXED, PERCENTAGE, 3 priority FROM PUPRIREG
WHERE PRICEGROUP = 'F' AND DISCOUNTLIST = '0013')
union
(SELECT DISCOUNT, 0 fixed, 0 percentage, 4 priority FROM PUPRIREG
WHERE PRICELIST = '01' AND PRICEGROUP = 'F')
order by priority;
The additional artificial priority column and order by ensures, that you get the discounts properly sorted.