簡體   English   中英

MySql中的COUNT個或字段總數

[英]COUNT or SUM of fields in MySql

我的數據庫中有一個表,最多可以保存60個字段,是或否。 我想計數或從表中的所有字段中減去總和,其中任何字段等於“ yes”;

我嘗試了以下方法,但是沒有運氣:

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

這將返回列數,但不允許我對等於“ yes”的字段進行計數。

我想知道這樣做在php中更容易嗎? 我目前正在codeigniter中執行此操作,並且我不知道如何讀取鍵/值,因為它是一個對象。 有任何想法嗎?

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

試試這個...。這個簡單的查詢將起作用..... :)

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

嘗試這個 :

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM