简体   繁体   English

MySql中的COUNT个或字段总数

[英]COUNT or SUM of fields in MySql

I have a table in my database which save up to 60 fields with a yes or a no. 我的数据库中有一个表,最多可以保存60个字段,是或否。 I am wanting to COUNT or get the SUM off all fields from the table WHERE any of the fields equal to 'yes'; 我想计数或从表中的所有字段中减去总和,其中任何字段等于“ yes”;

I have tried the following but no luck: 我尝试了以下方法,但是没有运气:

SELECT count(*) FROM information_schema.columns WHERE table_name = 'portraits';

This will return the column count but doesn't allow me to count the fields where they equal to 'yes'. 这将返回列数,但不允许我对等于“ yes”的字段进行计数。

I am wondering is this easier to do in php? 我想知道这样做在php中更容易吗? I am currently doing this in codeigniter and I don't know how to read the key/value as it is an object. 我目前正在codeigniter中执行此操作,并且我不知道如何读取键/值,因为它是一个对象。 Any ideas? 有任何想法吗?

SELECT count(*) 
FROM information_schema.columns 
    WHERE table_name = 'portraits'
    AND (field1=0 OR field2=0 OR .... OR fieldN=0);

Try this one.... this simple query gonna work..... :) 试试这个...。这个简单的查询将起作用..... :)

SELECT
SUM(IF( information_schema = "YES", 1, 0) AS Yes,
SUM(IF( information_schema != "YES", 1, 0) AS No, 
FROM portraits

Try this : 尝试这个 :

SELECT state, count(*) FROM information_schema.columns GROUP BY state;

Or in more complicated way : 或者以更复杂的方式:

SELECT y.total, n.total
  FROM (SELECT count(*) as total FROM information_schema.columns WHERE table_name = 'portraits' AND state = 'yes') as y,
       (SELECT count(*) as total FROM information_schema.columns WHERE table_name = 'portraits' AND state = 'no') as n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM