当至少满足 x 列中的条件时,MYSQL 选择行?

标签 mysql

另一个我找不到答案的 MYSQL 问题:

我有这张表,我想获取所有具有 SN>7 且代表次数 > 4 且在第 1-6 列中至少满足 x 次特定条件的 ID。

例如,列 col1-col6 中至少有 3 个单元格的值 > 1。

第一部分很简单(SELECT * FROM table WHERE SN > 7 AND reps > 4....),但我无法弄清楚第二部分。

谢谢!

ID SN    reps  col1   col2  col3  col4  col5  col6
A  12    3     0.6    1     3     -2    1     3
B  6     5     3.2    1.1   -3.3  3     0     0
C  300   6     1.3    -0.4  0     0.6   -0.5  -3.3

最佳答案

尝试:

SELECT * FROM table 
WHERE SN > 7 AND reps > 4 and
      (case when `1` > 1 then 1 else 0 end +
       case when `2` > 1 then 1 else 0 end +
       case when `3` > 1 then 1 else 0 end +
       case when `4` > 1 then 1 else 0 end +
       case when `5` > 1 then 1 else 0 end +
       case when `6` > 1 then 1 else 0 end) >= 3

关于当至少满足 x 列中的条件时,MYSQL 选择行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16223166/

相关文章:

php - 返回插入行的 id(自动递增)

php - 通过php在mysql表中插入多行

MySQL 计数行

mysql - 在 MySQL 中从 incode/outcode 计算持续时间的最佳方法是什么?

mysql - Codeigniter mysql程序多变量

php - Sphinxql 与 PDO php

php - 拉维尔 5 : How to do a join query on a pivot table using Eloquent?

mysql - 返回正确的staff_ids

PHP/MYSQL - 如何只为每个不同的值选择最近的值

mysql - 多对多失败, 'on clause' 中的未知列