介绍:
MySQL 是一个功能强大的关系型数据库管理系统,支持丰富的查询语句,用于从数据库中检索、插入、更新和删除数据。本文将介绍 MySQL 中常见和不常见的所有查询语句,并为每个语句提供示例。
常见查询语句:
1.select 从数据中检索数据
select * from table_name;
2.insert 向数据库表中插入新行
insert into table_name (column1, column2) values (value1, value2);
3.update 更新数据库表中的现有行
update table_name set column1 = value1 where condition;
4.delete 从数据库表中删除行
delete from table_name where condition;
5.where 用于过滤检索数据的条件
select * from table_name where column1 = 'value';
6.order by 对检索结果进行排序
select * from table_name order by column1 ASC;
7.group by 对检索结果进行分组
select column1, count(*) from table_name group by column1;
8.having 对 GROUP BY 子句返回的分组进行过滤
select column1, count(*) from table_name group by column1 having count(*) > 1;
9.join 用于将来自两个或多个表的行连接在一起
select * from table1 inner join table2 on table1.column = table2.column;
10.union 合并两个或多个 SELECT 语句的结果集
select column1 from table1 union select column1 from table2;
不常见但有用的查询语句:
1.rollup 生成多级总计行
select column1, sum(column2) from table_name group by rollup(column1);
2.pivot 将行数据转换为列数据
select * from (select column1, column2 from table_name) as src pivot (MAX(column2) for column1 IN ('value1', 'value2')) as pvt;
3.pilltext search 在文本列上执行全文搜索
select * from table_name where match(column1) against ('search_keyword');
4.recursive common table expressions 递归查询,允许在一个查询中引用相同的 CTE
with recursive cte as (select 1 as n union all select n + 1 from cte where n < 10) select * from cte;
5.windows functions 允许在结果集中的每一行上执行计算,通常与 ORDER BY 子句一起使用
select column1, sum(column2) over (partition by column3 order by column1) from table_name;
6.spatial runctions 用于地理空间数据类型的空间计算
select ST_Distance(point1, point2) from table_name;
7.json functions 用于在 JSON 数据上执行操作,如提取、修改和查询
select column1->'$.key' from table_name where column1->'$.key' = 'value';
8.merge 将两个表的数据合并在一起
merge into target_table using source_table on (target_table.id = source_table.id) when matched then update set target_table.column1 = source_table.column1 when not matched then insert (column1, column2) values (source_table.column1, source_table.column2);
以上是 MySQL 中常见和不常见的所有查询语句的示例。这些语句可以根据实际情况进行调整和扩展,以满足各种复杂的数据操作需求。
———————————————————————————————————————————
标签:语句,name,where,常见,MySQL,table,column1,select From: https://blog.csdn.net/AIBB_520/article/details/137493511