MySQL子句在`case when`的地方

时间:2018-04-19 09:33:18

标签: mysql mariadb

我想弄明白,但根本没有解决方案。 我的问题:

SELECT * FROM table
WHERE owner = 'user'
AND (
    CASE WHEN status = 'NEW'
    THEN status = 'NEW'
    ELSE status IN ('CB','NA','NI','GC','FC')
    END
)
ORDER BY RAND() DESC
LIMIT 1;

我想要实现的目的是显示status NEW Random orderone row每个受NEW限制的所有记录,当没有记录statuses时查询需要显示另一个from string import punctuation x = 'This is an example sentence.' table = str.maketrans('', '', punctuation) x = x.translate(table).lower().replace(' ', '') vowels = set('aeiou') consonants = sum(i not in vowels for i in x) vowels = sum(i in vowels for i in x) print(consonants) # 14 print(vowels) # 9 的用户。

谢谢。

3 个答案:

答案 0 :(得分:1)

SELECT * FROM Table
WHERE owner = 'user'
AND (CASE WHEN (SELECT COUNT(*) FROM Table WHERE status = 'NEW') >= 1 THEN
                status = 'NEW'
     ELSE status IN ('CB','NA','NI','GC','FC')
     END)
ORDER BY RAND() DESC
LIMIT 1;

<强>演示

  

http://sqlfiddle.com/#!9/09b1c8/1

答案 1 :(得分:0)

我认为,你可以简单地

cdo remapnn,lon=9.15/lat=36 input.nc output.nc

SELECT * FROM table
WHERE owner = 'user'
AND (
   CASE WHEN status = 'NEW'
   THEN status = 'NEW'
   ELSE status IN ('CB','NA','NI','GC','FC')
   END
)
ORDER BY RAND() DESC
LIMIT 1;

答案 2 :(得分:0)

SELECT * FROM table
    WHERE owner = 'user'
      AND status IN ('NEW', 'CB','NA','NI','GC','FC')
    ORDER BY status = 'NEW' DESC,
             RAND() DESC
LIMIT 1;

总共会给你一行,优先选择一个随机的&#39; NEW&#39;。如果没有&#39; NEW&#39;行,然后是随机的一行。

(请澄清您的问题并提供样本输出。)