基本 MySQL 查询:综合指南

WBOY
发布: 2024-08-24 09:12:19
转载
1007人浏览过

基本 mysql 查询:综合指南

作者:nirmalya mondal

介绍

mysql 是用于 web 应用程序和其他数据驱动应用程序的最流行的关系数据库管理系统 (rdbms) 之一。无论您是初学者还是想要提高 mysql 技能的人,了解基本查询都是至关重要的。本博客将引导您完成一些基本的 mysql 查询,可用于数据库操作、表操作和数据管理。

1. 数据库操作

创建数据库

首先,您需要一个数据库来存储表和数据。创建数据库很简单:

create database my_database;
登录后复制

选择数据库

创建数据库后,使用以下查询来选择它:

use my_database;
登录后复制

删除数据库

如果需要删除数据库,请使用以下命令:

drop database my_database;
登录后复制

2. 表操作

创建表

表是存储数据的地方。您可以创建包含特定列的表,如下所示:

create table users (
    id int auto_increment primary key,
    name varchar(100),
    email varchar(100),
    age int
);
登录后复制

显示表格

要查看所选数据库中的所有表:

show tables;
登录后复制

描述表结构

如果你想了解表的结构,可以描述一下:

describe users;
登录后复制

更改表

如果您需要通过添加或更改列来修改表格:

  • 添加专栏
  alter table users add phone varchar(15);
登录后复制
  • 修改列
  alter table users modify age tinyint;
登录后复制

掉落桌子

删除表:

drop table users;
登录后复制

3. 数据操作

插入数据

将数据添加到表中:

insert into users (name, email, age) values ('john doe', 'john@example.com', 25);
登录后复制

选择数据

从表中检索数据:

select name, email from users where age > 20;
登录后复制

选择所有数据

要检索表中的所有数据:

select * from users;
登录后复制

更新数据

更新表中的数据:

update users set age = 26 where name = 'john doe';
登录后复制

删除数据

要从表中删除数据:

delete from users where name = 'john doe';
登录后复制

4. 条件查询

where 子句

使用where子句根据特定条件过滤记录:

select * from users where age > 20;
登录后复制

和/或条件

使用 and 或 or 组合多个条件:

select * from users where age > 20 and name = 'john doe';
登录后复制

in 子句

根据值列表选择数据:

select * from users where age in (20, 25, 30);
登录后复制

between 子句

过滤一定范围内的数据:

select * from users where age between 20 and 30;
登录后复制

like条款

使用 like 子句搜索模式:

select * from users where name like 'j%';
登录后复制

is null / is not null

过滤具有 null 或 not null 值的记录:

select * from users where email is null;
登录后复制

5.聚合函数

count

计算行数:

select count(*) from users;
登录后复制

总和

计算列的总和:

select sum(age) from users;
登录后复制

avg

求一列的平均值:

select avg(age) from users;
登录后复制

最大和最小

查找一列的最大值或最小值:

select max(age) from users;
登录后复制
select min(age) from users;
登录后复制

6. 分组和排序

分组依据

根据一列或多列对数据进行分组:

select age, count(*) from users group by age;
登录后复制

拥有

过滤分组数据:

select age, count(*) from users group by age having count(*) > 1;
登录后复制

订购依据

按升序或降序对数据进行排序:

select * from users order by age desc;
登录后复制

7. 加入操作

内连接

从多个表中获取同时满足条件的数据:

select users.name, orders.order_date from users
inner join orders on users.id = orders.user_id;
登录后复制

左加入

从左表中获取数据并从右表中获取匹配的行:

select users.name, orders.order_date from users
left join orders on users.id = orders.user_id;
登录后复制

右加入

从右表中获取数据并从左表中获取匹配的行:

select users.name, orders.order_date from users
right join orders on users.id = orders.user_id;
登录后复制

8. 子查询

where 中的子查询

使用子查询来过滤结果:

select name from users where id = (select user_id from orders where order_id = 1);
登录后复制

select 中的子查询

使用子查询来计算值:

select name, (select count(*) from orders where users.id = orders.user_id) as order_count
from users;
登录后复制

9. 意见

创建视图

根据查询创建虚拟表:

create view user_orders as
select users.name, orders.order_date from users
inner join orders on users.id = orders.user_id;
登录后复制

下拉视图

删除视图:

drop view user_orders;
登录后复制

10. 索引

创建索引

通过创建索引提高查询性能:

create index idx_name on users (name);
登录后复制

掉落指数

删除索引:

DROP INDEX idx_name ON users;
登录后复制

结论

理解这些基本的 mysql 查询对于任何使用关系数据库的人来说都是至关重要的。无论您是管理数据、优化查询还是确保数据完整性,这些命令都构成了您的 mysql 技能的基础。通过掌握它们,您将能够轻松处理大多数与数据库相关的任务。

以上就是基本 MySQL 查询:综合指南的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号