【问题标题】:Price Comparision in mysql querymysql查询中的价格比较
【发布时间】:2014-01-26 13:50:58
【问题描述】:

我有一个价格:

$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 子句中执行此操作。

查询应该是什么样子?

【问题讨论】:

标签: mysql sql select case where-clause


【解决方案1】:

试试这个:

SELECT * FROM tableA
WHERE (CASE priceType WHEN 1 THEN price WHEN 2 THEN price * (800000 / 1000) END) <= $price

【讨论】:

  • 你能解释一下这个查询吗?
  • @CodeHunter 我的查询与您要求的相同
【解决方案2】:

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 . ")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    相关资源
    最近更新 更多