Home Database Mysql Tutorial Mysql中的count() 与sum()区别_MySQL

Mysql中的count() 与sum()区别_MySQL

Jun 01, 2016 pm 01:39 PM
20 count surface

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
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to update the latest version of Bybit Exchange? Will there be any impact if it is not updated? How to update the latest version of Bybit Exchange? Will there be any impact if it is not updated? Feb 21, 2025 pm 10:54 PM

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 difference between counta and count The difference between counta and count Nov 20, 2023 am 10:01 AM

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.

gateio official download address gateio official download Android version gateio official download address gateio official download Android version Feb 21, 2025 pm 03:00 PM

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.

Top 10 digital currency trading software (Latest update in 2025) Top 10 digital currency trading software (Latest update in 2025) Feb 14, 2025 pm 05:33 PM

As the digital currency market flourishes, buying and selling digital assets has become easier than ever. This article highlights the top ten digital currency trading software in 2025 that can help traders participate in the digital currency market easily and safely. We evaluate these software based on user-friendliness, security measures, fees and other key factors to provide a comprehensive guide to traders making informed decisions.

How to implement the statement to view table data in MySQL? How to implement the statement to view table data in MySQL? Nov 08, 2023 pm 01:40 PM

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

PEPU Coin Price Prediction for 2025 PEPU Coin Price Prediction for 2025 Dec 12, 2024 am 11:32 AM

PEPU Coin’s 2025 price prediction is based on a comprehensive analysis of technical, fundamentals, consensus, and statistical modeling. The forecast model takes into account factors such as market sentiment, regulatory environment, technological developments and macroeconomic factors that influence prices. Additionally, PEPU Coin can be compared to other cryptocurrencies such as BTC, ETH, BNB, USDC, and XRP to understand its investment potential and unique advantages. Further analysis also explores the PEPU coin’s potential value, long-term holding power and likelihood of reaching $1 within the next 5 years.

Top 10 Asian currency exchanges rankings 2025 latest rankings Top 10 Asian currency exchanges rankings 2025 latest rankings Feb 14, 2025 pm 11:12 PM

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.

Detailed tutorial on how to register for binance (2025 beginner's guide) Detailed tutorial on how to register for binance (2025 beginner's guide) Mar 18, 2025 pm 01:57 PM

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.

See all articles