[英]select query for multiple columns with same type of values
我有一個數據庫,我以這種方式將整數存儲在列中
1 1 1 1
1 1 1 1
1 0 1 0
0 1 2 2
因此,我想獲取例如值為1的每一列的計數。
我會非常感激的:)
我嘗試使用許多查詢,但是我不知道如何使用這種方式,我也檢查了流量和許多其他網站的堆棧,但是仍然以某種方式我仍然停留在此問題上。 我也嘗試過使用IN子句。
我已經嘗試通過使用OR和AND來使用select語句,但是我知道這將不起作用,並且會給出不正確的結果。
這取決於您的意思。 如果要計算值1在表格中出現的次數,可以使用count(*)
:
select
(select count(*) from myTable where col1 = 1) +
(select count(*) from myTable where col2 = 1) +
(select count(*) from myTable where col3 = 1) +
[...]
或者,您可以使用case
表達式:
select sum(*) from (
select
(case when col1 = 1 then 1 else 0 end) +
(case when col2 = 1 then 1 else 0 end) +
(case when col3 = 1 then 1 else 0 end)
from myTable
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.