mysql基本运算符
批量导入数据: mysql load data local infile 'e://source/student.txt' into table student lines terminated by '\r\n';mysql select * from student;+--------+----------+---------+-----------+| stu_id | stu_name | stu_tel | stu_score |+--------+
批量导入数据:
mysql> load data local infile 'e://source/student.txt' into table student lines terminated by '\r\n'; mysql> select * from student; +--------+----------+---------+-----------+ | stu_id | stu_name | stu_tel | stu_score | +--------+----------+---------+-----------+ | 1 | a | 151 | 60 | | 2 | b | 152 | 61 | | 3 | c | 153 | 62 | | 4 | d | 154 | 63 | | 5 | e | 155 | 64 | | 6 | a | 156 | 65 | | 7 | b | 157 | 66 | | 8 | c | 158 | 67 | | 9 | d | 159 | 68 | | 10 | e | 160 | 69 | +--------+----------+---------+-----------+
mysql> select *,max(stu_score) > from student > group by stu_name; +--------+----------+---------+-----------+----------------+ | stu_id | stu_name | stu_tel | stu_score | max(stu_score) | +--------+----------+---------+-----------+----------------+ | 1 | a | 151 | 60 | 65 | | 2 | b | 152 | 61 | 66 | | 3 | c | 153 | 62 | 67 | | 4 | d | 154 | 63 | 68 | | 5 | e | 155 | 64 | 69 | +--------+----------+---------+-----------+----------------+
group by 可以对包含两个或多个列进行分组。
mysql> select * from student; +--------+----------+---------+-----------+ | stu_id | stu_name | stu_tel | stu_score | +--------+----------+---------+-----------+ | 1 | a | 151 | 60 | | 2 | a | 152 | 60 | | 3 | a | 153 | 61 | | 4 | b | 154 | 62 | | 5 | b | 155 | 62 | | 6 | b | 156 | 63 | | 7 | c | 157 | 64 | | 8 | c | 158 | 64 | | 9 | c | 159 | 65 | +--------+----------+---------+-----------+ mysql> select *,count(stu_tel) from student group by stu_name,stu_score; +--------+----------+---------+-----------+----------------+ | stu_id | stu_name | stu_tel | stu_score | count(stu_tel) | +--------+----------+---------+-----------+----------------+ | 1 | a | 151 | 60 | 2 | | 3 | a | 153 | 61 | 1 | | 4 | b | 154 | 62 | 2 | | 6 | b | 156 | 63 | 1 | | 7 | c | 157 | 64 | 2 | | 9 | c | 159 | 65 | 1 | +--------+----------+---------+-----------+----------------+
在group by子句之后使用having运算符,对查询结果限定条件,系统仅返回满足条件的组结果。having子句可包含一个或多个用and和or连接的谓词。
mysql> select *,max(stu_score) as Max > from student > group by stu_name > having Max>65; +--------+----------+---------+-----------+------+ | stu_id | stu_name | stu_tel | stu_score | Max | +--------+----------+---------+-----------+------+ | 2 | b | 152 | 61 | 66 | | 3 | c | 153 | 62 | 67 | | 4 | d | 154 | 63 | 68 | | 5 | e | 155 | 64 | 69 | +--------+----------+---------+-----------+------+
mysql> select *,max(stu_score) as Max > from student > group by stu_name > having Max>65 > order by Max desc; +--------+----------+---------+-----------+------+ | stu_id | stu_name | stu_tel | stu_score | Max | +--------+----------+---------+-----------+------+ | 5 | e | 155 | 64 | 69 | | 4 | d | 154 | 63 | 68 | | 3 | c | 153 | 62 | 67 | | 2 | b | 152 | 61 | 66 | +--------+----------+---------+-----------+------+
mysql> select *,max(stu_score) as Max > from student > group by stu_name > having Max>65 > order by Max desc > limit 1,3; +--------+----------+---------+-----------+------+ | stu_id | stu_name | stu_tel | stu_score | Max | +--------+----------+---------+-----------+------+ | 4 | d | 154 | 63 | 68 | | 3 | c | 153 | 62 | 67 | | 2 | b | 152 | 61 | 66 | +--------+----------+---------+-----------+------+
mysql> select *, > case > when stu_score>'65' then '1' > when stu_score='65' then '2' > else '3' end as level > from student; +--------+----------+---------+-----------+-------+ | stu_id | stu_name | stu_tel | stu_score | level | +--------+----------+---------+-----------+-------+ | 1 | a | 151 | 60 | 3 | | 2 | b | 152 | 61 | 3 | | 3 | c | 153 | 62 | 3 | | 4 | d | 154 | 63 | 3 | | 5 | e | 155 | 64 | 3 | | 6 | a | 156 | 65 | 2 | | 7 | b | 157 | 66 | 1 | | 8 | c | 158 | 67 | 1 | | 9 | d | 159 | 68 | 1 | | 10 | e | 160 | 69 | 1 | +--------+----------+---------+-----------+-------+
like运算符:使用模式查找,其中百分号%代表任意0个、1个或多个任意字符,下划线_代表一个随机字符。若不用百分号或者下划线,like就相当于等于号=。
mysql> select * from student > where stu_tel like '_6%'; +--------+----------+---------+-----------+ | stu_id | stu_name | stu_tel | stu_score | +--------+----------+---------+-----------+ | 10 | e | 160 | 69 | +--------+----------+---------+-----------+
like的模式匹配中可以使用转义字符定义escape。escape可以定义任何字符为转移字符。如下例定义‘#’为转义字符,跟在‘#’后面的‘_’失去了原有意义。
mysql> select * from student > where stu_name like '%#_%' escape '#'; +--------+----------+---------+-----------+ | stu_id | stu_name | stu_tel | stu_score | +--------+----------+---------+-----------+ | 11 | a_b | 166 | 70 | +--------+----------+---------+-----------+

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

可以通过以下步骤打开 phpMyAdmin:1. 登录网站控制面板;2. 找到并点击 phpMyAdmin 图标;3. 输入 MySQL 凭据;4. 点击 "登录"。

MySQL是一种开源的关系型数据库管理系统,主要用于快速、可靠地存储和检索数据。其工作原理包括客户端请求、查询解析、执行查询和返回结果。使用示例包括创建表、插入和查询数据,以及高级功能如JOIN操作。常见错误涉及SQL语法、数据类型和权限问题,优化建议包括使用索引、优化查询和分表分区。

MySQL在数据库和编程中的地位非常重要,它是一个开源的关系型数据库管理系统,广泛应用于各种应用场景。1)MySQL提供高效的数据存储、组织和检索功能,支持Web、移动和企业级系统。2)它使用客户端-服务器架构,支持多种存储引擎和索引优化。3)基本用法包括创建表和插入数据,高级用法涉及多表JOIN和复杂查询。4)常见问题如SQL语法错误和性能问题可以通过EXPLAIN命令和慢查询日志调试。5)性能优化方法包括合理使用索引、优化查询和使用缓存,最佳实践包括使用事务和PreparedStatemen

选择MySQL的原因是其性能、可靠性、易用性和社区支持。1.MySQL提供高效的数据存储和检索功能,支持多种数据类型和高级查询操作。2.采用客户端-服务器架构和多种存储引擎,支持事务和查询优化。3.易于使用,支持多种操作系统和编程语言。4.拥有强大的社区支持,提供丰富的资源和解决方案。

Apache 连接数据库需要以下步骤:安装数据库驱动程序。配置 web.xml 文件以创建连接池。创建 JDBC 数据源,指定连接设置。从 Java 代码中使用 JDBC API 访问数据库,包括获取连接、创建语句、绑定参数、执行查询或更新以及处理结果。

在 Docker 中启动 MySQL 的过程包含以下步骤:拉取 MySQL 镜像创建并启动容器,设置根用户密码并映射端口验证连接创建数据库和用户授予对数据库的所有权限

在 CentOS 上安装 MySQL 涉及以下步骤:添加合适的 MySQL yum 源。执行 yum install mysql-server 命令以安装 MySQL 服务器。使用 mysql_secure_installation 命令进行安全设置,例如设置 root 用户密码。根据需要自定义 MySQL 配置文件。调整 MySQL 参数和优化数据库以提升性能。

优雅安装 MySQL 的关键在于添加 MySQL 官方仓库。具体步骤如下:下载 MySQL 官方 GPG 密钥,防止钓鱼攻击。添加 MySQL 仓库文件:rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm更新 yum 仓库缓存:yum update安装 MySQL:yum install mysql-server启动 MySQL 服务:systemctl start mysqld设置开机自启动
