
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.
mysql> 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()函数结合使用,从“学生”表中获取英语母语学生和非英语母语学生的输出。
Zend框架2是一个开源框架,使用PHP 5.3 +开发web应用程序和服务。Zend框架2使用100%面向对象代码和利用大多数PHP 5.3的新特性,即名称空间、延迟静态绑定,lambda函数和闭包。 Zend框架2的组成结构是独一无二的;每个组件被设计与其他部件数的依赖关系。 ZF2遵循SOLID面向对象的设计原则。 这样的松耦合结构可以让开发人员使用他们想要的任何部件。我们称之为“松耦合”
344
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()函数结合?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号