In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the relevant knowledge of "what is the conditional query method of count () in mybatis". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope that this article "what is the method of conditional query of count () in mybatis" can help you solve the problem.
Mybatis count () queries 1 and sql count () functions by condition
The count () function returns the number of rows that match the specified criteria.
Sql count (column_name) syntax:
The number of values returned by the count (column_name) function for the specified column (null) is not counted.
Select count (column_name) from table_name
Sql count (*) syntax:
The count (*) function returns the number of records in the table.
Select count (*) from table_name
Sql count (distinct column_name) syntax:
The count (distinct column_name) function returns the number of different values for the specified column.
Select count (distinct column_name) from table_name
For example, the following table: table_aid
+-+ | aid | site_id | count | date | +-+ | 1 | 1 | 45 | 2016-05-10 | 2 | 3 | 100 | 2016-05- | 13 | 3 | 1 | 2016 | 2016-05-14 | 4 | 2 | 10 | 2016-05-14 | 5 | 5 | 20 | 2016-05-14 | 6 | 4 | 13 | 2016-05-15 | 7 | 3 | 2016-05-15 | 8 | 5 | 545 | 2016-05-16 | 9 | 3 | 2016-05-17 | | | +-+
Execute the sql statement:
/ / number of specified columns under specific conditions select count (count) as nums from table_aidwhere site_id = 3Universe / output result: Nums value is: 521 / / calculate the total number of records in table_aid select count (*) as nums from table_aid// output result: Nums value is: 9 / / number of different values of specified column select count (distinct site_id) as nums from table_aid// output result: Nums value is: 52, count () in mybatis query by condition
Task description: two fields in the database are
1. Site: station,
2. Status: the value of status,status is success or fail.
Now the requirement is to group records by site, and count the number of status as success and the number as fail.
Mybatis Code:
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
The test results are as follows:
{
"failNum": 2
"totalNum": 73
"successNum": 71
"station": "admin"
}
{
"failNum": 26
TotalNum: 521
"successNum": 495
"station": "changjiu.shao@wisdom56.com"
}
When querying, use count (*), total is 1, and the result is 0
When using the count (*) query, it is found that the total returned by the mybatis log printed on console is 1, but the actual situation should be 0, and the returned data is also 0.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.