mysql语句中select sum into
时间: 2023-11-04 10:33:45 浏览: 150
SELECT SUM(column_name) INTO variable_name FROM table_name;
This statement selects the sum of values in a specific column of a table and stores the result in a variable. The variable can be used later in the code or displayed to the user.
相关问题
Mysql语句整理大全
MySQL语句整理大全如下:
1. 创建数据库:
```
CREATE DATABASE database_name;
```
2. 删除数据库:
```
DROP DATABASE database_name;
```
3. 切换到指定数据库:
```
USE database_name;
```
4. 创建数据表:
```
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
.....
);
```
5. 删除数据表:
```
DROP TABLE table_name;
```
6. 插入数据:
```
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
```
7. 更新数据:
```
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
```
8. 删除数据:
```
DELETE FROM table_name WHERE condition;
```
9. 查询数据:
```
SELECT column1, column2, ... FROM table_name WHERE condition;
```
10. 查询数据并排序:
```
SELECT column1, column2, ... FROM table_name WHERE condition ORDER BY column1 ASC|DESC, column2 ASC|DESC, ...;
```
11. 查询数据并限制返回结果的数量:
```
SELECT column1, column2, ... FROM table_name WHERE condition LIMIT num;
```
12. 查询数据并分页:
```
SELECT column1, column2, ... FROM table_name WHERE condition LIMIT start, num;
```
13. 查询数据并计算统计值:
```
SELECT COUNT(column_name) FROM table_name WHERE condition;
```
14. 查询数据并计算平均值:
```
SELECT AVG(column_name) FROM table_name WHERE condition;
```
15. 查询数据并计算最大值:
```
SELECT MAX(column_name) FROM table_name WHERE condition;
```
16. 查询数据并计算最小值:
```
SELECT MIN(column_name) FROM table_name WHERE condition;
```
17. 查询数据并计算总和:
```
SELECT SUM(column_name) FROM table_name WHERE condition;
```
这些是MySQL语句的常见用法,希望对你有帮助。
mysql查询语句大全
MySQL 查询语句涵盖了从基本的数据检索到复杂的数据操作的各种场景。这里列举一些常用的SQL查询语句:
1. **SELECT**:用于获取数据的基本语句,如 `SELECT * FROM table_name` (获取表中的所有列),`SELECT column1, column2 FROM table_name` (指定列)。
2. **WHERE**:筛选满足特定条件的记录,例如 `SELECT * FROM table_name WHERE condition`,如 `WHERE age > 18`。
3. **JOIN**:连接两个或更多表,如 `INNER JOIN`, `LEFT JOIN`, `RIGHT JOIN`, 或 `FULL OUTER JOIN`。
4. **GROUP BY**:对结果集按某个字段分组,通常与聚合函数(如 COUNT(), SUM(), AVG() 等)一起使用。
5. **ORDER BY**:按照指定列排序查询结果,例如 `ORDER BY column_name ASC/DESC`。
6. **LIMIT**:限制返回的行数,如 `SELECT * FROM table_name LIMIT offset, count`。
7. **INSERT INTO**:插入新记录,如 `INSERT INTO table_name (column1, column2) VALUES (value1, value2)`。
8. **UPDATE**:更新现有记录,如 `UPDATE table_name SET column = value WHERE condition`。
9. **DELETE**:删除记录,如 `DELETE FROM table_name WHERE condition`。
10. **COUNT()**, **SUM()**, **AVG()**, **MAX()**, **MIN()**: 分别计算行数、数值总和、平均值、最大值和最小值。
阅读全文