如果这个问题是愚蠢的,我很抱歉。我想做的是:-

<---'type' is a parameter of my procedure--->
SELECT * FROM `table1` WHERE
  CASE type
    WHEN 'some' THEN `column1`='column1string'
    WHEN 'somestr' THEN `column2` = 'column2string' AND `column1` = 'column1string'
    WHEN 'somestring' THEN `column3` = 'column3string'
  END IF;

但我想在使用case语句时编写更多的sql查询:-
CASE type
 WHEN 'some' THEN SELECT * FROM `table1` WHERE `coumn1`='column1string';
 WHEN 'somestr' THEN SELECT * FROM `table1` WHERE `coumn2`='column2string' AND `column1` = 'column1string';
WHEN 'somestring' THEN SELECT * FROM `table1` WHERE `coumn2`='column2string';
END CASE;

我怎么能不写同样的查询更多次。因为我有40个这样的字符串来比较。

最佳答案

我想你可以用布尔逻辑:

SELECT t.*
FROM `table1` t
WHERE (type = 'some' AND `column1` = 'column1string') OR
      (type = 'somestr' AND `coumn2` = 'column2string') OR
      (type = 'somestring' AND `coumn2` = 'column2string');

关于mysql - 你能建议像这样的mysql功能吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47793761/

10-13 09:36