[英]Price Comparision in mysql query
I have a price: 我有一个价格:
$price = 100;
I have 2 Database fields 我有2个数据库字段
1) priceType
2) price
Now the cases are: 现在的情况是:
if priceType =1
CASE 1: price <= $price // Do this comparison in WHere Clause
if priceType =2
CASE 2: price* (800000 / 1000) <= '$price' // Do this comparison in WHere Clause
Only one operation should be performed at a time, depends on priceType. 一次只能执行一个操作,具体取决于priceType。
If priceType = 1 than the $price should be compared in first case way. 如果priceType = 1,则应该以第一种情况比较$ price。
If priceType = 2 than the $price should be compared in second case way. 如果priceType = 2,则应使用第二种情况比较$ price。
I want to perform this in Where Clause. 我想在Where子句中执行此操作。
What should the query look like ? 查询应该是什么样?
Try this: 尝试这个:
SELECT * FROM tableA
WHERE (CASE priceType WHEN 1 THEN price WHEN 2 THEN price * (800000 / 1000) END) <= $price
or with IF
: 或
IF
:
SELECT
IF (priceType = 1, price,
IF (priceType = 2, price * (800000 / 1000),
'unknown priceType'
)
) AS price
FROM priceTable
Update: 更新:
I maybe missunderstood... 我可能会误会...
SELECT price
FROM priceTable
WHERE (priceType = 1 AND price <= " . $price . ")
OR (priceType = 2 AND (price* (800000 / 1000)) <= " . $price . ")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.