我有个价:
$price = 100;
我有2个数据库字段
1) priceType
2) price
现在的案例是:
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
一次只能执行一项操作,具体取决于priceType。
如果priceType = 1,则应以第一种方式比较$ price。
如果priceType = 2,则应以第二种方式比较$ price。
我想在Where子句中执行此操作。
查询应该是什么样的?
答案 0 :(得分:1)
试试这个:
SELECT * FROM tableA
WHERE (CASE priceType WHEN 1 THEN price WHEN 2 THEN price * (800000 / 1000) END) <= $price
答案 1 :(得分:0)
或IF
:
SELECT
IF (priceType = 1, price,
IF (priceType = 2, price * (800000 / 1000),
'unknown priceType'
)
) AS price
FROM priceTable
更新
我可能会被误解......
SELECT price
FROM priceTable
WHERE (priceType = 1 AND price <= " . $price . ")
OR (priceType = 2 AND (price* (800000 / 1000)) <= " . $price . ")