Mysql中的count() 与sum()区别_MySQL
bitsCN.com
首先创建个表说明问题
CREATE TABLE `result` (
`name` varchar(20) default NULL,
`subject` varchar(20) default NULL,
`score` tinyint(4) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8
插入一些数据,
insert into result values
('张三','数学',90),
('张三','语文',50),
('张三','地理',40),
('李四','语文',55),
('李四','政治',45),
('王五','政治',30),
('赵六','语文',100),
('赵六','数学',99),
('赵六','品德',98);
要求:查询出2门及2门以上不及格者的平均成绩。
经常会用两种查询语句有两种:
1. select name,sum(score =2;
2.select name ,count((score=2;
两种查询的结果肯定是不一样的,肯定是第一种正确的,原因是为什么,这时你就要想了,count()函数的意义和sum函数的意义
count()函数里面的参数是列名的的时候,那么会计算有值项的次数。
Sum()函数里面的参数是列名的时候,是计算列名的值的相加,而不是有值项的总数。
对count()行数还要注意:它会计算总行数。不管你是否有值都会列入计算范围。另外一点:mysqlisam引擎很容易获得总行数的统计。查询速度变得更快
归纳:实际编程中统计总行数是经常用到的。此时使用count(*)多处可见。我很少看到有人使用列名作为参数:count(a)的情况。即使是这样使用,可能其初衷也是想统计行数。只是不知道!这样所造成的细微差异而错误使用了"列名"的形式。
bitsCN.com

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The way to update ByBit exchanges varies by platform and device: Mobile: Check for updates and install in the app store. Desktop Client: Check for updates in the Help menu and install automatically. Web page: You need to manually access the official website for updates. Failure to update the exchange can lead to security vulnerabilities, functional limitations, compatibility issues and reduced transaction execution efficiency.

The Count function is used to count the number of numbers in a specified range. It ignores text, logical values, and null values, but counts empty cells. The Count function only counts the number of cells that contain actual numbers. The CountA function is used to count the number of non-empty cells in a specified range. It not only counts cells containing actual numbers, but also counts the number of non-empty cells containing text, logical values, and formulas.

With the booming virtual currency market, cryptocurrency exchanges in Asia are also rising rapidly. This article compiles the latest rankings of the top ten exchanges in the Asian currency circle in 2025, providing investors with an authoritative reference for choosing a reliable trading platform. Ranking is comprehensively evaluated based on multiple dimensions such as transaction volume, asset quantity, security and user experience, aiming to help investors select the best exchange that suits their trading needs.

Title: Statements and specific code examples for viewing table data in MySQL MySQL is an open source relational database management system that is widely used in applications of all sizes. In MySQL, viewing table data is a very basic operation. The following will introduce how to implement this operation through specific statements and code examples. First, we will introduce the statements and specific code examples for viewing table data through the MySQL command line tool. Suppose we have a table named "employees", the following is the pass

Cryptocurrency Exchange Rankings 2025: Binance (Advantages: Large trading volume, multiple currencies, friendly interface, and complete derivatives market) OKX (Advantages: low fees, multiple trading pairs, professional trading tools) Huobi (Advantages: long history, good liquidity, Chinese interface) Bybit (Advantages: focus on derivatives, low fees, novice-friendly) Kraken (Advantages: good security measures, high compliance, wide altcoin support)

Gate.io provides an official mobile application that allows users to download the Android version through the following steps: visit the official website, click the "Download" button, select the "Android" option, scan the QR code or click the "Google Play" link. Currently, Gate.io does not provide an official iOS mobile app. Users can access the website through a mobile web browser or install the Android version on iOS devices through an Android emulator or third-party services.

This article provides a complete guide to Binance registration and security settings, covering pre-registration preparations (including equipment, email, mobile phone number and identity document preparation), and introduces two registration methods on the official website and APP, as well as different levels of identity verification (KYC) processes. In addition, the article also focuses on key security steps such as setting up a fund password, enabling two-factor verification (2FA, including Google Authenticator and SMS Verification), and setting up anti-phishing codes, helping users to register and use the Binance Binance platform for cryptocurrency transactions safely and conveniently. Please be sure to understand relevant laws and regulations and market risks before trading and invest with caution.

MySQL is a commonly used relational database management system that supports the operation of renaming tables. Normally, renaming a table carries certain risks, so you should be very careful when performing this operation. In this article, we will explore how to implement the rename table statement in MySQL and provide detailed code examples. In MySQL, you can use the ALTERTABLE statement to rename a table. The following is the basic syntax of the ALTERTABLE rename statement: ALTERTABLEo
