Hello! 欢迎来到盒子萌!

mysql数据查询


avatar
dspirit 2025-01-30 39

[每天记录一个问题,记录十年 Blog]

1.1 mysql 查询数据

select column1,column2,···  # 列1,列2,是需要选择的列的名称,如果使用 * 表示选择所有的列
from table_name  # table_name是要查询数据的表的名称
[where condition]  # where是可选子句,用于过滤条件,只返回符合条件的行
[order by coumn_name [asc | desc]]  # [asc | desc是可选子句,用于指定结果集的排序,默认是升序ASC]
[limit number];  # limit number 是一个可选的子句,用于限制返回的行数

实例:

select * from users;  # 选择所有列所有行
select username,email from users;  # 选择特定所有行
select * from users where is_active = true;  # where子句,选择满足条件的行
selest * from users order by birthdate desc;  # order by子句,按指定列降序排序
select * from users limit 10;  # limit子句,限制返回的行数

进阶实例:

select * from users where username like 'j%' and is_active = true;  # and运算符和通配符
select * from users where is_active = true or birthdate < '1990-01-01';  # or运算符
select * from users where birthdate in ('1990-01-01','1992-03-15','1993-05-03');  # in子句
查询语句中可以使用一个或者多个表,表之间使用逗号, 分割,并使用WHERE语句来设定查询条件。
你可以在 WHERE 子句中指定任何条件。
你可以使用 AND 或者 OR 指定一个或多个条件。
WHERE 子句也可以运用于 SQL 的 DELETE 或者 UPDATE 命令。
WHERE 子句类似于程序语言中的 if 条件,根据 MySQL 表中的字段值来读取指定的数据。

A. where子句(=/>=/<=,and/or,like,in,not,is null,is not null)

select * from user where username = 'test';  # 等于条件
select * from user where username != 'runoob';  # 不等于条件
select * from order where order_date <= '2023-01-01'  # 小于等于条件

select * from products where category = 'Electronics' and/or price > 100.00  # 组合and/or
select * from customers where first_name like 'J%'  # 模糊匹配条件like
select * from countries where country_code in ('US','CA','MX');  # in条件
select * from products where not category = 'Clothing';  # not条件

select * from orders where order_date between '2023-01-01' and '2023-12-31';  # between
select * from mployees where departments is null;  # is null条件
select * from customers where email is not null;  # is not null条件

执行顺序:

阶段/子句 作用说明 关键点与注意
from & join 指定查询的数据源表,并根据 on 条件连接多个表,生成初始的中间结果集。 查询的起点,所有操作的基础。on 条件在此阶段应用。
where fromjoin 后的中间结果集进行行级过滤。 不能使用 select 中的别名或聚合函数。应在此阶段尽可能过滤掉最多数据,以提高性能。
group by 根据指定的列对过滤后的数据进行分组,为聚合运算做准备。 分组后,每组将产生结果集中的一条记录。
having group by 分组后的结果集进行组级过滤。 只能用于过滤聚合条件。与 where 的区别:where 过滤行,having 过滤组。
select 计算表达式、指定要返回的列、执行聚合函数、定义列别名。 虽然写在最前面,但此时才执行。可以使用前序阶段已有的列和计算的聚合值。
distinct select 返回的结果集中去除重复的行。 如果查询包含 distinct,它会在 select 之后立即应用。
(窗口函数) 如果使用了窗口函数(如 row_number()),它们在此阶段进行计算。 窗口函数在执行完聚合后,但在最终排序和限制之前处理。
union/intersect/except 如果查询是集合操作(如合并两个查询结果),在此阶段执行。 用于合并多个 select 语句的结果集。
order by 对最终的结果集按照指定的列进行排序。 可以使用在 select 阶段定义的别名。排序通常很耗时。
limit / offset 限制返回的行数和指定返回结果的起始点,常用于分页。 整个查询的最后一步。在排序后截取最终需要返回给客户端的部分结果。

1.2 mysql 更新数据

实例:

update employees
set salary = 60000
where employee_id = 101;  # 更新单个列的值
update orders
set status = 'shipped', ship_date = '2023-03-01'
where order_id = 1001;  # 更新多个列的值
update products
set price = price * 1.1
where category = 'electronics';  # 使用表达式更新值
update students
set status = 'graduated';  # 更新符合条件的所有行,以上 sql 语句将所有学生的状态更新为 'graduated'。
update customers
set total_purchases = (
    select sum(amount)
    from orders
    where orders.customer_id = customers.customer_id
)
where customer_type = 'premium';  # 更新使用子查询的值
# 以上 sql 语句通过子查询计算每个 'premium' 类型客户的总购买金额,并将该值更新到 total_purchases 列中。

1.3 删除数据

实例:

delete from students
where graduation_year = 2021;  # 删除符合条件的行
delete from orders;  # 删除所有行,删除了 orders 表中的所有记录,但表结构保持不变
delete from customers
where customer_id in (
    select customer_id
    from orders
    where order_date < '2023-01-01'
);  # 子查询删除符合条件的行,删除了 orders 表中在 '2023-01-01' 之前下的订单对应的客户

1.4 like子句

select * from customers where last_name like 's%';  # 'a%' 匹配以字母 'a' 开头的任何字符串
select * from products where product_name like '_a%';  # '_r%' 匹配第二个字母为'r'的任何字符串
select * from users where username like 'a%o_';
# 匹配以字母 'a' 开头,然后是零个或多个字符,接着是 'o',最后是一个任意字符的字符串
select * from employees where last_name like 'smi%' collate utf8mb4_general_ci;
# 选择姓氏以 'smi' 开头的所有员工,不区分大小写

1.5 union

select city from customers
union
select city from suppliers
order by city;  # 选择客户表和供应商表中所有城市的唯一值,并按城市名称升序排序
select product_name from products where category = 'electronics'
union
select product_name from products where category = 'clothing'
order by product_name;  # 选择电子产品和服装类别的产品名称,并按产品名称升序排序
select first_name, last_name from employees
union
select department_name, null from departments
order by first_name;  # union 操作中的列数和数据类型必须相同
# 返回的结果包括了 employees 表中的 first_name 和 last_name,以及 departments 表中的 department_name 和 null, 所有的结果都按照 first_name 列排序
select city from customers
union all
select city from suppliers
order by city;  # 使用 union all 不去除重复行,将客户表和供应商表中的所有城市合并在一起,不去除重复行

相关阅读