0

我的查询:

SELECT *
FROM ranks
WHERE
    (price = 25.00 AND accumulate = 0)
    OR
    (price <= (SELECT SUM(amount) FROM donations WHERE username = 'username' AND amount IN (SELECT price FROM ranks WHERE accumulate = 1)))
ORDER BY price
DESC LIMIT 1

基本上,如果找到匹配项,我想返回第一个 where 子句,否则返回第二个子句。

4

2 回答 2

1

此查询将起作用

SELECT *
FROM ranks
WHERE price = 25.00 AND accumulate = 0

UNION ALL

SELECT *
FROM ranks
WHERE NOT EXISTS(SELECT *
                  FROM ranks
                  WHERE price = 25.00 AND accumulate = 0)
      AND (price <= (SELECT SUM(amount) FROM donations WHERE username = 'username' AND        amount IN (SELECT price FROM ranks WHERE accumulate = 1)))
ORDER BY price
DESC LIMIT 1
于 2013-01-28T06:10:10.890 回答
0

用 IF 试试这个查询

SELECT *
FROM ranks
WHERE if(price = 25.00,accumulate = 0, price <= (SELECT
                           SUM(amount)
                         FROM donations
                         WHERE username = 'username'
                             AND amount IN(SELECT
                                     price
                                   FROM ranks
                                   WHERE accumulate = 1)))
ORDER BY price DESC
LIMIT 1
于 2013-01-28T06:08:14.127 回答