当前位置: 首页 > article >正文

SQL自学,mysql从入门到精通 --- 第 5 天,对函数的处理

对函数的处理

新建一个成绩表

root@mysqldb 09:39:  [d1]> create table score (
    -> name varchar(30),
    -> chinese int,
    -> math int,
    -> music int,
    -> team int,
    -> magic int,
    -> computer int
    -> );
Query OK, 0 rows affected (0.01 sec)

root@mysqldb 09:39:  [d1]> insert into score
    -> values
    -> ("1A","90","85","30","92","70","65"),
    -> ("2B","99","98","90","95","92","89"),
    -> ("3C","90","70","60","88","89","92"),
    -> ("4D","89","85","59","78","93","94"),
    -> ("5E","88","70","66","92","58","76");
Query OK, 5 rows affected (0.06 sec)
Records: 5  Duplicates: 0  Warnings: 0

-- 数据如下
root@mysqldb 09:44:  [d1]> select * from score;
+------+---------+------+-------+------+-------+----------+
| name | chinese | math | music | team | magic | computer |
+------+---------+------+-------+------+-------+----------+
| 1A   |      90 |   85 |    30 |   92 |    70 |       65 |
| 2B   |      99 |   98 |    90 |   95 |    92 |       89 |
| 3C   |      90 |   70 |    60 |   88 |    89 |       92 |
| 4D   |      89 |   85 |    59 |   78 |    93 |       94 |
| 5E   |      88 |   70 |    66 |   92 |    58 |       76 |
+------+---------+------+-------+------+-------+----------+
5 rows in set (0.00 sec)


COUNT 统计表中有多少行数据

-- COUNT(*):统计表中的所有行数,包括所有列的数据行
root@mysqldb 09:46:  [d1]> select count(*) from score;
+----------+
| count(*) |
+----------+
|        5 |
+----------+
1 row in set (0.00 sec)

-- COUNT(column_name):统计指定列中非 NULL 值的行数
root@mysqldb 09:47:  [d1]> select count(chinese) from score;
+----------------+
| count(chinese) |
+----------------+
|              5 |
+----------------+
1 row in set (0.00 sec)

-- COUNT(DISTINCT column_name):统计指定列中不同值的行数。
root@mysqldb 09:49:  [d1]> select count(distinct chinese) from score;
+-------------------------+
| count(distinct chinese) |
+-------------------------+
|                       4 |
+-------------------------+
1 row in set (0.00 sec)

-- COUNT(DISTINCT column_name1, column_name2):统计多个列中不同值的组合行数

SUM 返回某一列所有数值的总和

-- 统计math列的总和
root@mysqldb 09:55:  [d1]> SELECT SUM(math) AS total_math FROM score;
+------------+
| total_math |
+------------+
|        408 |
+------------+
1 row in set (0.01 sec)

-- 统计多个列数值的总和
root@mysqldb 09:56:  [d1]> SELECT SUM(math) total_math, SUM(team) total_team FROM score;
+------------+------------+
| total_math | total_team |
+------------+------------+
|        408 |        445 |
+------------+------------+
1 row in set (0.00 sec)

-- 求数学成绩的平均分
root@mysqldb 09:57:  [d1]> SELECT <

http://www.kler.cn/a/539725.html

相关文章:

  • 视觉硬件选型和算法选择(CNN)
  • “深入浅出”系列之C++:(18)C++11
  • trimesh 加载obj mesh处理
  • antd pro常见代码示例-ProTable
  • android apk反编译
  • node.js + html + Sealos容器云 搭建简易多人实时聊天室demo 带源码
  • 神经网络|(九)概率论基础知识-泊松分布及python仿真
  • MySQL与钉钉数据融合,加速企业付款退款自动化进程
  • Spring AI -使用Spring快速开发ChatGPT应用
  • 鸿蒙NEXT API使用指导之文件压缩和邮件创建
  • 【Spring】Spring MVC入门(一)
  • 如何将 Jupyter Notebook (.ipynb) 文件转换为 Python (.py) 文件
  • Git 常见错误与解决方案全指南
  • 安装perl-DBD-MySQL报错:Requires: libmysqlclient.so.18
  • C++ 设计模式 - 访问者模式
  • MySQL数据库 - 阶段性体系总结
  • 【Vue3路由小技巧】用Hash模式打造流畅体验!
  • 【kafka系列】Topic 与 Partition
  • x小兔鲜vue.js
  • C#中的Frm_Welcome.Instance.Show(),是什么意思
  • c++ 输入输出笔记
  • 流氓软件一键屏蔽免疫工具Baidun Armor v3.2.1 绿色版
  • Centos Ollama + Deepseek-r1+Chatbox运行环境搭建
  • DeepSeek从入门到精通教程PDF清华大学出版
  • 【CubeMX+STM32】SD卡 文件系统读写 FatFs+SDIO+DMA
  • Java语言的区块链