MySQL 常见和不常见的所有查询语句

介绍:

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 中常见和不常见的所有查询语句的示例。这些语句可以根据实际情况进行调整和扩展,以满足各种复杂的数据操作需求。

———————————————————————————————————————————

相关推荐

  1. MySQL 常见常见所有查询语句

    2024-04-09 18:26:04       17 阅读
  2. MySQL 中,常见 JOIN 查询语句

    2024-04-09 18:26:04       13 阅读
  3. mysql一些常用查询语句

    2024-04-09 18:26:04       19 阅读
  4. 一些常见MySQL问题答案

    2024-04-09 18:26:04       19 阅读
  5. 列举常见排序查找算法

    2024-04-09 18:26:04       7 阅读
  6. 常见查找算法

    2024-04-09 18:26:04       35 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-09 18:26:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-09 18:26:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-09 18:26:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-09 18:26:04       20 阅读

热门阅读

  1. 自己总结的ICT云计算题库三

    2024-04-09 18:26:04       14 阅读
  2. 【Leetcode】【2024048】1544. Make The String Great

    2024-04-09 18:26:04       14 阅读
  3. react api:createContext

    2024-04-09 18:26:04       15 阅读
  4. go 获取terraform output输出

    2024-04-09 18:26:04       14 阅读
  5. 5.2 SSH和交换机端口安全概述

    2024-04-09 18:26:04       12 阅读