您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

MySQL聚合函数如何与MySQL IF()函数结合?

2025/5/23 17:24:11发布46次查看
combining mysql aggregate functions with mysql if() function can be very helpful to get the specific output we want. consider the following queries which combine sum() and count() aggregate functions with if() function.
examplemysql> select sum(if(language = 'english', 1, 0)) as english, sum(if(language <> 'english',1,0)) as "non-english" from students;+---------+-------------+| english | non-english |+---------+-------------+| 5 | 4 |+---------+-------------+1 row in set (0.00 sec)
上述查询将sum()聚合函数与if()函数结合使用,从“学生”表中获取英语母语学生和非英语母语学生的输出。
mysql> select count(if(country = 'usa', 1, null))as usa, -> count(if(country = 'uk', 1, null))as uk, -> count(if(country = 'france', 1, null))as france, -> count(if(country = 'russia', 1, null))as russia, -> count(if(country = 'australia', 1, null))as australia, -> count(if(country = 'india', 1, null))as india, -> count(if(country = 'nz', 1, null))as nz from students;+-----+----+--------+--------+-----------+-------+----+| usa | uk | france | russia | australia | india | nz |+-----+----+--------+--------+-----------+-------+----+| 2 | 1 | 1 | 1 | 1 | 2 | 1 |+-----+----+--------+--------+-----------+-------+----+1 row in set (0.07 sec)
上面的查询将count()聚合函数与if()函数结合起来,以从“students”表中获取国家数量的输出。
以上就是mysql聚合函数如何与mysql if()函数结合?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product