我的数据库中有一个表,可以保存多达60个字段,表示是或否。我想要COUNT或从表中的所有字段中获取SUM WHERE任何字段等于'yes';
我尝试过以下但没有运气:
SELECT count(*) FROM information_schema.columns WHERE table_name = 'portraits';
这将返回列数,但不允许我计算它们等于“是”的字段。
我想知道这在php中更容易吗?我目前在codeigniter中这样做,我不知道如何读取键/值,因为它是一个对象。有什么想法吗?
答案 0 :(得分:0)
SELECT count(*)
FROM information_schema.columns
WHERE table_name = 'portraits'
AND (field1=0 OR field2=0 OR .... OR fieldN=0);
答案 1 :(得分:0)
尝试这个....这个简单的查询将起作用..... :)
SELECT
SUM(IF( information_schema = "YES", 1, 0) AS Yes,
SUM(IF( information_schema != "YES", 1, 0) AS No,
FROM portraits
答案 2 :(得分:0)
试试这个:
SELECT state, count(*) FROM information_schema.columns GROUP BY state;
或者以更复杂的方式:
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