MySQL高级SQL语句

围绕两张表

Location表

Store_Info表

 #select选择
 SELECT Store_Name FROM Store_Info;
 #distinct去重
 select distinct 列名 from 表名
 #where条件查询
 select distinct 列名 from 表名 where
 #and且 or或
 语法:SELECT "字段" FROM "表名" WHERE "条件1" {[AND|OR] "条件2"}+ ;
 SELECT Store_Name FROM Store_Info WHERE Sales > 1000 OR (Sales < 500 AND Sales > 200);
 #in显示已知的
 not in ≈取反
 语法:SELECT "字段" FROM "表名" WHERE "字段" IN ('值1', '值2', ...);
 SELECT * FROM Store_Info WHERE Store_Name IN ('Los Angeles', 'Houston');
 #between两个值范围内的数据记录
 select * from store_info where between 300 and 1000;
 300≤结果≤1000
 ​
 #通配符——配合like使用
 %:百分号表示零个、一个或多个字符
 _:下划线表示单个字符
 ​
 #like
 语法:SELECT "字段" FROM "表名" WHERE "字段" LIKE {模式};
 SELECT * FROM Store_Info WHERE Store_Name like '%os%';
 ​
 #order by
 asc升序
 desc降序
 语法:SELECT "字段" FROM "表名" [WHERE "条件"] ORDER BY "字段" [ASC, DESC];
 #ASC 是按照升序进行排序的,是默认的排序方式。
 #DESC 是按降序方式进行排序。
 SELECT Store_Name,Sales,Date FROM Store_Info ORDER BY Sales DESC;
 函数
 数学函数:
 abs(x) 返回 x 的绝对值
 rand() 返回 0 到 1 的随机数
 mod(x,y) 返回 x 除以 y 以后的余数
 power(x,y) 返回 x 的 y 次方
 round(x) 返回离 x 最近的整数
 round(x,y) 保留 x 的 y 位小数四舍五入后的值
 sqrt(x) 返回 x 的平方根
 truncate(x,y) 返回数字 x 截断为 y 位小数的值
 ceil(x) 返回大于或等于 x 的最小整数
 floor(x) 返回小于或等于 x 的最大整数
 greatest(x1,x2...) 返回集合中最大的值,也可以返回多个字段的最大的值
 least(x1,x2...) 返回集合中最小的值,也可以返回多个字段的最小的值
 SELECT abs(-1), rand(), mod(5,3), power(2,3), round(1.89);
 SELECT round(1.8937,3), truncate(1.235,2), ceil(5.2), floor(2.1), least(1.89,3,6.1,2.1);
 ​
 聚合函数:
 avg() 返回指定列的平均值
 count() 返回指定列中非 NULL 值的个数
 min() 返回指定列的最小值
 max() 返回指定列的最大值
 sum(x) 返回指定列的所有值之和
 ​
 SELECT avg(Sales) FROM Store_Info;
 ​
 SELECT count(Store_Name) FROM Store_Info;
 SELECT count(DISTINCT Store_Name) FROM Store_Info;
 ​
 SELECT max(Sales) FROM Store_Info;
 SELECT min(Sales) FROM Store_Info;
 ​
 SELECT sum(Sales) FROM Store_Info;
 字符串函数
 trim() 返回去除指定格式的值
 concat(x,y) 将提供的参数 x 和 y 拼接成一个字符串
 substr(x,y) 获取从字符串 x 中的第 y 个位置开始的字符串,跟substring()函数作用相同
 substr(x,y,z) 获取从字符串 x 中的第 y 个位置开始长度为 z 的字符串
 length(x) 返回字符串 x 的长度
 replace(x,y,z) 将字符串 z 替代字符串 x 中的字符串 y
 upper(x) 将字符串 x 的所有字母变成大写字母
 lower(x) 将字符串 x 的所有字母变成小写字母
 left(x,y) 返回字符串 x 的前 y 个字符
 right(x,y) 返回字符串 x 的后 y 个字符
 repeat(x,y) 将字符串 x 重复 y 次
 space(x) 返回 x 个空格
 strcmp(x,y) 比较 x 和 y,返回的值可以为-1,0,1
 reverse(x) 将字符串 x 反转
 ​
 a=12345678
 echo $(a:6:3)
 echo $(#a)
 GROUP BY配合“聚合函数”一起使用
 ​
 HAVING 对group by汇总后的结果做筛选

select store_name ,sum(store_name) from store_info group by store_name having sum(store_name) >=2

连接查询

 ​
 select * from location as A inner join store_info as B on A.store_name = B.store_name;
 ​

表连接

inner join 内连接,只返回两个表的字段相等的行记录

left join 左连接,返回左表所有的行记录和右表字段相等的行记录,不相等的行返回null

right join 右连接,返回右表所有的行记录和左表字段相等的行记录,不相等的行返回null

union 联集,将两个select查询语句的结果合并,并去重

union all 联集,将两个select查询语句的结果合并,不去重

求交集

select A.字段 from 左表 A inner join 右表 B

 数据库模式:
 create database kgc;
 ​
 show databases;
 ​
 use kgc;
 ​
 create table location (Region char(20),Store_Name char(20));
 ​
 desc location;
 ​
 insert into location values('East','Boston');
 insert into location values('East','New York');
 insert into location values('West','Los Angeles');
 insert into location values('West','Houston');
 ​
 select * from location;
 ​
 create table store_info (Store_Name char(20),Sales int(10),Date char(10));
 desc store_info;
 ​
 insert into store_info values('Los Angeles','1500','2020-12-05');
 insert into store_info values('Houston','250','2020-12-07');
 insert into store_info values('Los Angeles','300','2020-12-08');
 insert into store_info values('Boston','700','2020-12-08');
 ​
 select * from store_info;
 ​
 select * from store_info where store_name='Los Angeles';
 ​
 select * from store_info where sales <= 1000;
 ​
 select * from store_info where sales >= 1000;
 ​
 select * from store_info where sales != 1500;
 ​
 select * from store_info where sales > 1000 or (sales < 500 and sales > 200);
 ​
 select * from store_info where Store_Name in ('Los Angeles', 'Houston');
 ​
 select * from store_info where date between '2020-12-06' and '2020-12-10';
 ​
 select * from store_info where store_name like '%os%';
 ​
 select store_name,sales,date from store_info order by sales desc;
 ​
 select sum(sales) from store_info;
 ​
 select min(sales) from store_info;
 ​
 select max(sales) from store_info;
 ​
 select avg(sales) from store_info;
 ​
 select count(sales) from store_info;
 ​
 select concat('abc','123');
 ​
 select concat('abc',' ','123');
 ​
 ​
 select * from location;
 ​
 select * from store_info;
 ​
 select * from location where store_name='New York';
 ​
 ​
 select concat(Region,store_name) from location where store_name='New York';
 ​
 select concat(Region,'+',store_name) from location where store_name='New York';
 ​
 select region || store_name from location;
 ​
 select region || ' ' || store_name from location;
 ​
 select substr(store_name,5) from location where store_name='Los Angeles';
 ​
 select substr(store_name,5,6) from location where store_name='Los Angeles';
 ​
 select store_name, sum(sales) from store_info group by store_name order by sales desc;
 ​
 select store_name, sum(sales) from store_info group by store_name having sum(sales) > 1500;
 ​
 ​
 select a.store_name store, sum(a.sales) "total sales" from store_info a group by a.store_name;
 ​
 select sum(sales) from store_info where store_name in (select store_name from location where region = 'west');
 ​
 select sum(a.sales) from store_info a where a.store_name in (select store_name from location b where b.store_name = a.store_name);
 ​
 select sum(sales) from store_info where exists (select * from location where region = 'West');
 ​
 update store_info set store_name='Washington' WHERE sales=300;
 ​
 select * from store_info;
 ​
 ​
 select * from location a right join store_info b on a.store_name = b.store_name ;
 ​
 select * from location a left join store_info b on a.store_name = b.store_name ;
 ​
 ​
 select * from location a inner join store_info b on a.store_name = b.store_name ;
 ​
 select * from location a, store_info b where a.store_name = b.store_name;
 ​
 select a.region region, sum(b.sales) sales from location a, store_info b where a.store_name = b.store_name group by region;
 ​
 select store_name from location union select store_name from store_info;
 ​
 select store_name from location union all select store_name from store_info;
 ​
 select A.store_name from location A inner join store_info B ON A.store_name = B.store_ore_name;
 ​
 select A.store_name from location A inner join store_info B using(store_name);
 

MySQL高级SQL语句的更多相关文章

  1. 29.MySQL高级SQL语句

    MySQL高级SQL语句 目录 MySQL高级SQL语句 创建两个表 SELECT DISTINCT WHERE AND OR IN BETWEEN 通配符 LIKE ORDER BY 函数 数学函数 ...

  2. MySQL 数据库SQL语句——高阶版本2

    MySQL 数据库SQL语句--高阶版本2 实验准备 数据库表配置: mysql -uroot -p show databases; create database train_ticket; use ...

  3. mysql 常用 sql 语句 - 快速查询

    Mysql 常用 sql 语句 - 快速查询 1.mysql 基础 1.1 mysql 交互         1.1.1 mysql 连接             mysql.exe -hPup    ...

  4. Mysql 常用 SQL 语句集锦

    Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...

  5. Mysql 常用 SQL 语句集锦 转载(https://gold.xitu.io/post/584e7b298d6d81005456eb53)

    Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...

  6. MySQL数据库sql语句的一些简单优化

    1.查询条件的先后顺序 有多个查询条件时,要把效率高能更精确筛选记录的条件放在后边.因为MySQL解析sql语句是从后往前的(不知是否准确). 例: select a.*,b.* from UsrIn ...

  7. mysql下sql语句 update 字段=字段+字符串

    mysql下sql语句 update 字段=字段+字符串   mysql下sql语句令某字段值等于原值加上一个字符串 update 表明 SET 字段= 'feifei' || 字段; (postgr ...

  8. MySQL数据库SQL语句基本操作

    一.用户管理: 创建用户: create user '用户名'@'IP地址' identified by '密码'; 删除用户: drop user '用户名'@'IP地址'; 修改用户: renam ...

  9. mysql执行sql语句过程

    开发人员基本都知道,我们的数据存在数据库中(目前最多的是mysql和oracle,由于作者更擅长mysql,所以这里默认数据库为mysql),服务器通过sql语句将查询数据的请求传入到mysql数据库 ...

  10. MySQL与SQL语句的操作

    MySQL与SQL语句的操作 Mysql比较轻量化,企业用的是Oracle,基本的是熟悉对数据库,数据表,字段,记录的更新与修改 1. mysql基本信息 特殊数据库:information_sche ...

随机推荐

  1. 在HTML中引入React和JSX

    前言 Vue 可以非常方便地与 Pure HTML 结合,代替 jQuery 的功能,有一次遇到类似的场景时,我就想 React 能不能也以这种方式接入 HTML 网页,从而提高开发效率. 结果当然是 ...

  2. 巧用 awk 批量杀进程

    今天遇到线上的一个问题: 我需要批量杀死某台机器的 PHP 进程,该怎么办? 注意,不是 php-fpm,是常驻任务. 如果是一个进程,那就好办了,ps -ef | grep php,找到 PID 然 ...

  3. 2023年郑州轻工业大学校赛邀请赛clk

    需要总结的地方挺多的,首先是题目一次通过率有待提高,对于一些特别的样例还要加以分析,算法熟练的不高,不能清晰的看出在哪道题考什么算法,就比如兔子爱吃萝卜那道题,就是一个背包问题,比较基础,但是我们团队 ...

  4. 简单解决jsp中文乱码问题

    简单解决jsp中文乱码问题 初学jsp制作一个简单的响应页面 具体代码如下: <form action="test.jsp"> username : <input ...

  5. docker安装8版本elasticsearch遇到的问题FileSystemException

    docker安装8版本elasticsearch遇到的问题 Exception in thread "main" java.nio.file.FileSystemException ...

  6. AI识别检验报告 -PaddleNLP UIE-X 在医疗领域的实战

    目录 UIE-X在医疗领域的实战 1.项目背景 2.案例简介 3.环境准备 数据转换 5.模型微调 6.模型评估 7.Taskflow一键部署 UIE-X在医疗领域的实战 PaddleNLP全新发布U ...

  7. Linux 设置 VI 快捷键 -- 在多个打开的文件中切换

    场景 部署完一系列服务后,想要查看所有服务的 catelina.out 日志: vi $(find /data/http | grep catalina.out | grep -v bak) 这个命令 ...

  8. ch-manager.sh

    [root@dev-clickhouse1 ~]# cat ch-manager.sh #!/bin/bash ch_arr=(ch1-shard1-main ch1-shard2-sub ch2-s ...

  9. 写一段python下载商品图片的代码

    以下是一个简单的Python代码示例,用于下载商品图片: import requests import os def download_image(url, save_path): response ...

  10. Redis从入门到放弃(5):事务

    1.事务的定义 Redis的事务提供了一种"将多个命令打包, 然后一次性.按顺序地执行"的机制. redis事务的主要作用就是串联多个命令防止别的命令插队. 但是,事务并不具有传统 ...