我只想知道计数列是否可行,因为您在不调用每个列名的情况下计算行数。例如:
SELECT COUNT(FOUND COLUMNS) AS counted FROM table WHERE a_value EXISTS IN COLUMNS
/*
The same as this specified one. This is working but I'm
looking for another approach without calling each column name.
SELECT (IF(col1="Y",1,0) + IF(col2="Y",1,0)) as counted FROM table
*/
我希望每行都会抛出结果,如:
| counted |
|-----------|
| 2 |
| 1 |
| 0 |
是否可能?如果是,怎么样?
答案 0 :(得分:0)
TABLE_SIZE
试试这个它会起作用。
答案 1 :(得分:0)
我认为你正在寻找那个
THE SCHEMA
CREATE TABLE `numbers`( `col1` INT, `col2` INT, `col3` INT, `col4` INT );
INSERT INTO `numbers` (`col1`, `col2`, `col3`, `col4`) VALUES ('0', '0', '0', '6');
INSERT INTO `numbers` (`col1`, `col2`, `col3`, `col4`) VALUES ('1', '0', '0', '9');
INSERT INTO `numbers` (`col1`, `col2`, `col3`, `col4`) VALUES ('4', '1', '0', '2');
INSERT INTO `numbers` (`col1`, `col2`, `col3`, `col4`) VALUES ('9', '5', '1', '3');
INSERT INTO `numbers` (`col1`, `col2`, `col3`, `col4`) VALUES ('5', '0', '6', '4');
INSERT INTO `numbers` (`col1`, `col2`, `col3`, `col4`) VALUES ('2', '0', '1', '6');
INSERT INTO `numbers` (`col1`, `col2`, `col3`, `col4`) VALUES ('8', '5', '6', '7');
计算每行的值'0'
SELECT (c1+c2+c3+c4) AS counted FROM
(
SELECT
CASE WHEN col1 = 0 THEN 1 ELSE 0 END AS c1,
CASE WHEN col2 = 0 THEN 1 ELSE 0 END AS c2,
CASE WHEN col3 = 0 THEN 1 ELSE 0 END AS c3,
CASE WHEN col4 = 0 THEN 1 ELSE 0 END AS c4
FROM numbers
) AS temp_count
/ 结果 /
计数
3 2 1 0 1 1 0
计算每行的“7”值
SELECT (c1+c2+c3+c4) AS counted FROM
(
SELECT
CASE WHEN col1 = 7 THEN 1 ELSE 0 END AS c1,
CASE WHEN col2 = 7 THEN 1 ELSE 0 END AS c2,
CASE WHEN col3 = 7 THEN 1 ELSE 0 END AS c3,
CASE WHEN col4 = 7 THEN 1 ELSE 0 END AS c4
FROM numbers
) AS temp_count
因此,如果要检查其他值,则必须替换col1 = 7
,col2 = 7
,col3 = 7
,col4 = 7
的值。