mysql 查詢不同列的數量合計

2022-08-02 11:27:09 字數 1144 閱讀 4346

車輛違規資訊表testmodel_test

表結構:

表字段:cra_id(車牌號),if_weigui(該次行駛是否違規,0是正常,1是違規)

目的:

查詢表中共有幾輛車,違規的有幾輛車;

方法1

select count(f.cra_id) carnum, sum(f.weigui) weiguinum from

(select a.cra_id,b.weigui

from testmodel_test a

left join

(select t.cra_id, 1 as weigui from testmodel_test t

where t.if_weigui=1

group by t.cra_id) b on b.cra_id=a.cra_id

group by a.cra_id)f

方法2

select table1.車輛數,table2.違規車輛數 from

(select count(distinct(cra_id)) as '車輛數' from `testmodel_test` )table1

join

(select count(distinct(cra_id)) as '違規車輛數' from `testmodel_test` where if_weigui=1)table2

如果是有多行資料,可以加個鏈結控制

select table1.車輛數,table2.違規車輛數 from

(select0 as a,count(distinct(cra_id)) as '車輛數' from `testmodel_test` )table1

join

(select 0 as c ,count(distinct(cra_id)) as '違規車輛數' from `testmodel_test` where if_weigui=1)table2

on table1.a=table2.c

結果:

mysql 合計 mysql 查詢不同列的數量合計

車輛違規資訊表testmodel test 表結構 表字段 cra id 車牌號 if weigui 該次行駛是否違規,0是正常,1是違規 目的 查詢表中共有幾輛車,違規的有幾輛車 方法1select count f.cra id carnum,sum f.weigui weiguinum from...

不同列獲取等級數量

相關貼子 use tempdb goif object id t is not null drop table t gocreate table t id int 姓名 nvarchar 10 語文 nvarchar 10 數學 nvarchar 10 物理 nvarchar 10 化學 nvarc...

利用MySQL統計一列中不同值的數量方法示例

前言 在一張表中,需要統計其中一列資料中,出現重複的記錄數。解決方案1 select count from table where origin 條件1 select count from user operation log where origin 條件2 select count from u...