Get price between like 0-5000 and 50001-10000 from price column? - php

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

Related

MySql query to get non sold products from inventory table

I have 2 tables in my project. One is 'Products' another is 'Inventory Movement'.
'Products' table is just a master table with all our products loaded. It has only 2 fields
id - Auto increment field for product id
product_name - VarChar 255 unique column for product name
'Inventory Movement' table looks like below
id - Auto increment field
movement_type - Enum field (IN, OUT)
product - id from product table
quantity - No of items moving
movement_time - timestamp of action
Sample Data of movement table...
id movement_type product Quantity movement_time
1 IN 1001 10 2018-02-01 12:35:33
2 IN 1002 15 2018-02-01 13:33:33
3 OUT 1001 5 2018-03-01 11:00:33
now i have to take a report which list all products with the difference in time between its last 'OUT' and last 'IN' type records.
for a single product i wrote query like this...
1) Getting the last 'OUT' record;
select movement_time from inventory_movement where product = '1001' and movement_type='OUT' order by movement_time desc limit 1
2) Getting the last 'IN' record;
select movement_time from inventory_movement where product = '1001' and movement_type='IN' order by movement_time desc limit 1
3) Finding the diff between 2 timestamps.
this is how i know the unsold duration of the product with its id.
i know this is ineffective way... is there any way i can do this in single query ?
Is there anyway i get the list of products with this timestamp difference along with that ?
i.e.. the following is the expected output...
Product ---- Time Difference between last Out and last In
1001 ---- 40 days
1002 ---- 45 days
1004 ---- 12 days
Is it possible with single query ?
Seems that a DATEDIFF between a conditional MIN and MAX should do.
SELECT product,
DATEDIFF(MAX(case when movement_type='OUT' then movement_time end), MIN(case when movement_type='IN' then movement_time end)) AS InOutDayDiff
from inventory_movement
GROUP BY product
ORDER BY product
You can test it here

Search in table which contain multiple types of currencies

I have these tables
Currencies Table
Columns : curn_I'd, curn_name, curn_rate
Products Table
Columns : pro_I'd, pro_price, pro_currency -- Pro_currency in Products Table come from curn_id in the Currencies table
This way for example I have the following case :
Product table's data as
Id Price Currency
1 100 2
2 200 5
3 300 1
4 400 5
5 500 4
This mean I have many products with different prices and different currencies type.
How can I search for products where price between 200 and 400 dollars.
I need someway to convert all prices into dollars before query the data base then search in all products.
The following SQL query will join currencies and products table by mataching their currencies and will list down product id with product price in respective currencies.
SELECT
p.id, p.pro_price * c.curn_rate, c.curn_id
FROM
products p, currencies c
WHERE
p.pro_currency = c.curn_id;
I am given answer to consider as SQlserver ( I don't have knowledge about MYSQl, you just convert or think logic of this query)
--create table
declare #currency table (id int, curn_name varchar(50) , curr_rate float )
declare #product table (id int, price float , currency int )
--insert data into table
insert into #currency values (1, 'Dollar', 67.22) , (2,'Euro' ,75.38 )
insert into #product values (1,100,2),(2,200,5), (3,300,1),(4,400,5), (5,500,4)
--select query to see data
select * from #currency
select * from #product
--This query gives you desired result
select p.Id, curn_name CurrencyName, curr_rate CurrentCurrencyRate, p.price ProductPrice, p.price * c.curr_rate DollarPrice
from #product p -- give always alies
JOIn #currency c on p.currency = c.id
Output Result
Id CurrencyName CurrentCurrencyRate ProductPrice DollarPrice
3 Dollar 67.22 300 20166
1 Euro 75.38 100 7538
The below query list all distinct products with their prices(considering currency rate is given in dollars):
SELECT
DISTINCT pro.id, pro.pro_price*cur.curn_rate AS price
FROM
products AS pro, currencies AS cur
WHERE
pro.pro_currency = cur.curn_id
AND
pro.pro_price * cur.curn_rate >= 200
AND
pro.pro_price * cur.curn_rate <=400
Query in ANSI format:
SELECT
DISTINCT pro.Id, pro.pro_price*cur.curn_rate AS price
FROM
products AS pro
INNER JOIN
currencies AS cur
ON pro.pro_currency = cur.curn_Id
WHERE
pro.pro_price * cur.curn_rate >= 200
AND
pro.pro_price * cur.curn_rate <=400

How to use order by for discounted prices

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

get product price based on customer code

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.

MYSQL sort function, depending on the arithmetic operation using the database field values as array keys

I have table of products, and there are 2 fields: price, currency. Possible values in currency fields are 1,2,3. 1 for MDL, 2 for USD, and 3 for EUR. I have to sort my products by price in mdl currency. So i have an array with rates:
$v = array(1,11.38,15.8);
Help my please with my query, I've tried something like this, but I've got errors:
$results = $this->Product
->query("SELECT `id`,`price`,`currency` FROM products
ORDER BY price*$v[currency] DESC");
Hmm... I`ll try to explain, through an example.
My table:
id|price|currency
_________________
1 | 500 | 2
2 | 300 | 3
It shows that first products price is saved in USD, and second products price is saved in EUR. But i have to sort them in MDL valute. So i get an array of rates for each value:
$rates = array([1] = > 1, [2] => 11.50, [3] => 15.50);
So i have to order my products by the result of formula: price*value rate
in first case: 500*$rates['currency value from db, in our case 2] = 500 * 11.50 etc.
Thanks in advance.
Because of the extended example on this problem I have edited this query.
Lets assume that the currencies are alse stored in some table, lets say currency (if not, it should be anyway).
Table currency should be as follows:
ID VALUE CODE
-----------------------------
1 1 USD
2 11.38 EUR
3 15.8 MDL
Then the query should be:
SELECT p.`id`, p.`price`, p.`price` * c.`value` AS 'ratio'
FROM products p
LEFT JOIN currency c ON c.`id` = p.`currency`
ORDER BY `ratio` DESC
By this query You select the currency value from the table currency depending on the currency ID from products table and finaly the results are ordered by the ration price * currency value.
I understand that maybe You have the currencies only hardcoded as array within some config, but it really would be better to put the currencies into the DB (if it is not).
You can`t use data base column name as array keys, because mysql is later instance than php. In php you simply generate query string that is passed to database managment system.
Your query should look like this:
$results = $this->Product->query
(
"SELECT `id`,`price`,
CASE `currency`
WHEN '1' THEN $v[0]
WHEN '2' THEN $v[1]
WHEN '3' THEN $v[2]
END AS 'ratio'
FROM products
ORDER BY price*ratio DESC
"
);

Categories