Mysql 如何从表格中选择偶数数值

Mysql 如何从表格中选择偶数数值,mysql,sql,Mysql,Sql,我有一个名为nums的mySql表,在下面的结构中填充了数字数据 id, n1, n2, n3, n4, n5, n6 01, 34, 11, 07, 32, 19, 08 02, 17, 03, 45, 02, 06, 15 .., .., .., .., .., .., .. 我的目标是能够选择一行中有多少奇数和多少偶数 我已经完成了下面的奇数选择语句,但我不确定如何对偶数进行等效,如有任何建议,将不胜感激。谢谢 示例:选择有多少行有3个奇数 SET @Number

我有一个名为nums的mySql表,在下面的结构中填充了数字数据

id, n1, n2, n3, n4, n5, n6    
01, 34, 11, 07, 32, 19, 08    
02, 17, 03, 45, 02, 06, 15
.., .., .., .., .., .., ..

我的目标是能够选择一行中有多少奇数和多少偶数

我已经完成了下面的奇数选择语句,但我不确定如何对偶数进行等效,如有任何建议,将不胜感激。谢谢


示例:选择有多少行有3个奇数

  SET @NumbersOfOddColumnsWanted = 3;

  SELECT 
    n1, n2, n3, n4, n5, n6
  FROM
     nums
  WHERE
    n1 % 2 + n2 % 2 + n3 % 2 + n4 % 2 + n5 % 2 + n6 % 2 = @NumbersOfOddColumnsWanted

MySQL有一个很好的特性,“布尔”值被视为0或1。所以,你可以把它们加起来

SELECT ((n1%2 = 0) + (n2%2 = 0) + (n3%2 = 0) + (n4%2 = 0) + (n5%2 = 0) + (n6%2 = 0)
       ) as NumEvens,
       ((n1%2 = 1) + (n2%2 = 1) + (n3%2 = 1) + (n4%2 = 1) + (n5%2 = 1) + (n6%2 = 1)
       ) as NumOdds
FROM nums;

这假设所有的值都不是空的

MySQL有一个很好的特性,即“布尔”值被视为0或1。所以,你可以把它们加起来

SELECT ((n1%2 = 0) + (n2%2 = 0) + (n3%2 = 0) + (n4%2 = 0) + (n5%2 = 0) + (n6%2 = 0)
       ) as NumEvens,
       ((n1%2 = 1) + (n2%2 = 1) + (n3%2 = 1) + (n4%2 = 1) + (n5%2 = 1) + (n6%2 = 1)
       ) as NumOdds
FROM nums;

这假设所有的值都不是空的

MySQL有一个很好的特性,即“布尔”值被视为0或1。所以,你可以把它们加起来

SELECT ((n1%2 = 0) + (n2%2 = 0) + (n3%2 = 0) + (n4%2 = 0) + (n5%2 = 0) + (n6%2 = 0)
       ) as NumEvens,
       ((n1%2 = 1) + (n2%2 = 1) + (n3%2 = 1) + (n4%2 = 1) + (n5%2 = 1) + (n6%2 = 1)
       ) as NumOdds
FROM nums;

这假设所有的值都不是空的

MySQL有一个很好的特性,即“布尔”值被视为0或1。所以,你可以把它们加起来

SELECT ((n1%2 = 0) + (n2%2 = 0) + (n3%2 = 0) + (n4%2 = 0) + (n5%2 = 0) + (n6%2 = 0)
       ) as NumEvens,
       ((n1%2 = 1) + (n2%2 = 1) + (n3%2 = 1) + (n4%2 = 1) + (n5%2 = 1) + (n6%2 = 1)
       ) as NumOdds
FROM nums;

这假设所有值均为
NULL

那么
6-@NumbersOfOddColumnsWanted
呢?那么
6-@NumbersOfOddColumnsWanted
呢?那么
6-@NumbersOfOddColumnsWanted
呢?非常感谢@Gordon Linoff,正是我所需要的汉克斯@Gordon Linoff,正是我所需要的汉克斯@Gordon Linoff,正是我所需要的汉克斯@Gordon Linoff,正是我所需要的