最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mybatis中的count()按條件查詢方式

 更新時(shí)間:2022年01月15日 11:27:54   作者:光陰不負(fù)卿  
這篇文章主要介紹了mybatis中的count()按條件查詢方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

mybatis count()按條件查詢

1、sql count()函數(shù)

count()函數(shù)返回匹配指定條件的行數(shù)。

sql count(column_name)語法:

count(column_name)函數(shù)返回指定列的值的數(shù)目(null)不計(jì)入。

select count(column_name) from table_name

sql count(*)語法:

count(*)函數(shù)返回表中的記錄數(shù)。

select count(*) from table_name

sql count(distinct column_name)語法:

count(distinct column_name)函數(shù)返回指定列的不同值的數(shù)目。

select count(distinct column_name) from table_name

比如下面這張表:table_aid 

+-----+---------+-------+------------+
| aid | site_id | count | date ? ? ? |
+-----+---------+-------+------------+
| ? 1 | ? ? ? 1 | ? ?45 | 2016-05-10 |
| ? 2 | ? ? ? 3 | ? 100 | 2016-05-13 |
| ? 3 | ? ? ? 1 | ? 230 | 2016-05-14 |
| ? 4 | ? ? ? 2 | ? ?10 | 2016-05-14 |
| ? 5 | ? ? ? 5 | ? 205 | 2016-05-14 |
| ? 6 | ? ? ? 4 | ? ?13 | 2016-05-15 |
| ? 7 | ? ? ? 3 | ? 220 | 2016-05-15 |
| ? 8 | ? ? ? 5 | ? 545 | 2016-05-16 |
| ? 9 | ? ? ? 3 | ? 201 | 2016-05-17 |
+-----+---------+-------+------------+

執(zhí)行sql語句:

//特定條件下指定列的數(shù)目
select count(count) as nums from table_aid
where site_id = 3
//輸出結(jié)果:nums值為:521
?
//計(jì)算table_aid中總記錄數(shù)
select count(*) as nums from table_aid
//輸出結(jié)果:nums值為:9
?
//指定列的不同值的數(shù)目
select count(distinct site_id) as nums from table_aid
//輸出結(jié)果:nums值為:5

2、mybatis中count()按條件查詢

任務(wù)描述:數(shù)據(jù)庫其中有兩個(gè)字段分別為

1、站點(diǎn):station、

2、狀態(tài):status,status的取值為success或者fail。

現(xiàn)在需求為將記錄按站點(diǎn)分組,且要統(tǒng)計(jì)出其中的status為success的數(shù)量和為fail的數(shù)量。

mybatis代碼:

 <resultMap id="GroupBaseResultMap" type="java.util.Map">
? ? <result column="station" jdbcType="VARCHAR" property="station" />
? ? <result column="successNum" jdbcType="VARCHAR" property="successNum" />
? ? <result column="totalNum" jdbcType="VARCHAR" property="totalNum"/>
? </resultMap>
? <!--任務(wù)統(tǒng)計(jì):按站點(diǎn) ? ?sum(if(type="fail",status,0))-->
? <select id="selectGroupByStation" resultMap="GroupBaseResultMap">
? ? select rfr.station, count(rfr.status='success' or null) as successNum, count(rfr.status='fail' or null) as failNum, count(1) as totalNum
? ? from rec_file_report rfr
? ? GROUP BY station
? </select>

測(cè)試結(jié)果為:

    {
        "failNum": 2,
        "totalNum": 73,
        "successNum": 71,
        "station": "admin"
    },
    {
        "failNum": 26,
        "totalNum": 521,
        "successNum": 495,
        "station": "changjiu.shao@wisdom56.com"
    }

在查詢時(shí)使用count(*),total為1,結(jié)果為0

在使用count(*)查詢時(shí),發(fā)現(xiàn)在console打印的mybatis日志返回的total為1,但是實(shí)際情況應(yīng)該是0,返回的數(shù)據(jù)也是0

<== Total: 1

最后才發(fā)現(xiàn),在使用count(*)查詢時(shí),返回的total并不是查詢結(jié)果,即使為0,返回的也是1,跟total沒有關(guān)系。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

麻阳| 宜州市| 老河口市| 卢湾区| 广饶县| 平江县| 建始县| 柳州市| 芦溪县| 共和县| 疏附县| 东光县| 土默特左旗| 延庆县| 即墨市| 佛教| 五家渠市| 铜陵市| 六安市| 招远市| 泰州市| 镇康县| 成武县| 祁门县| 岑溪市| 正阳县| 新平| 金塔县| 个旧市| 凌源市| 中西区| 图木舒克市| 内乡县| 江津市| 上饶市| 垣曲县| 彰武县| 东莞市| 绿春县| 滦平县| 巍山|