2sql
------------------------------------ 高级查询
-- as 起别名
select name as 名字 from studnets;
-- 消除重复的行 -- 查看有哪几种xxx
select distinct gender from students; -- 查看有哪几种性别
select distinct name,gender from students; -- 查看有哪几种(性别,姓名)组合
------ where 运算符
---- 比较
-- 等于: =
-- 大于: >
-- 大于等于: >=
-- 小于: <
-- 小于等于: <=
-- 不等于: != 或 <>
select * from students where id <= 4;
---- 逻辑
-- and or not
select * from students where id < 4 or is_delete=0;
-------- 模糊查询
-- like
-- %表示任意多个任意字符
-- _表示一个任意字符
select * from students where name like 'M%';
select * from students where name like 'Mik_';
-------- 范围查询
-- in表示在一个非连续的范围内
select * from students where id in (1, 3);
-- between ... and ...表示在一个连续的范围内
select * from students where cls_id between 1 and 3; -- 1,2,3
--------- 空判断
select * from students where cls_id is not null;
--------------------------------------------------------排序
select * from students order by name asc默认/desc;
select * from students [where age in (10, 11)] order by name; -- 先拿到数据集再排序
--------------------------------------------------------聚合函数
select count(*) from students;
-- select max min avg
select sum(age) from students;
-------------------------------------------------------- 分组
select gender from students group by gender;
-- +--------+
-- | gender |
-- +--------+
-- | 男 |
-- | 女 |
-- | 中性 |
-- | 保密 |
-- +--------+
-------------------- group by + group_concat()
select gender, group_concat(name) from students group by gender;
-- +--------+-----------------------------------------------------------+
-- | gender | group_concat(name) |
-- +--------+-----------------------------------------------------------+
-- | 男 | 彭于晏,刘德华,周杰伦,程坤,郭靖 |
-- | 女 | 小明,小月月,黄蓉,王祖贤,刘亦菲,静香,周杰 |
-- | 中性 | 金星 |
-- | 保密 | 凤姐 |
-- +--------+-----------------------------------------------------------+
-------------------- group by + 集合函数
-- 分别统计性别为男/女的人年龄平均值
select gender,avg(age) from students group by gender;
-------------------- group by + having 过滤
-- 平均年龄大于10的性别
select gender, avg(age) from students group by gender having avg(age) > 10;
-- 人数大于1的性别和人数
select gender, count(*) from students group by gender having count(*) > 1;
-------------------- group by + with rollup 汇总
---- with rollup的作用是:在最后新增一行,来记录当前列里所有记录的总和
select gender,count(*) from students group by gender with rollup;
----------------------------------------分页
select * from students limit 3; -- 取前3条,相当于0,3
---- select * from 表名 limit start,count
select * from students limit 2, 4; -- 跳过2条 从第3条开始取4条
---------------------------------------- 链接查询
select * from classes as c [inner] join students as s on c.id = s.cls_id;
-- +----+-----------+----+------+------+--------+--------+--------+
-- | id | name | id | name | age | height | gender | cls_id |
-- +----+-----------+----+------+------+--------+--------+--------+
-- | 1 | 精英班 | 1 | Mike | 10 | 170.00 | 男 | 1 |
-- | 1 | 精英班 | 2 | John | 11 | 180.00 | 男 | 1 |
-- +----+-----------+----+------+------+--------+--------+--------+
select * from classes as c left join students as s on c.id = s.cls_id;
-- +----+-----------+------+------+------+--------+--------+--------+
-- | id | name | id | name | age | height | gender | cls_id |
-- +----+-----------+------+------+------+--------+--------+--------+
-- | 1 | 精英班 | 1 | Mike | 10 | 170.00 | 男 | 1 |
-- | 1 | 精英班 | 2 | John | 11 | 180.00 | 男 | 1 |
-- +----+-----------+------+------+------+--------+--------+--------+
select * from classes as c right join students as s on c.id=s.cls_id;
-- +------+-----------+----+------+------+--------+--------+--------+
-- | id | name | id | name | age | height | gender | cls_id |
-- +------+-----------+----+------+------+--------+--------+--------+
-- | 1 | 精英班 | 1 | Mike | 10 | 170.00 | 男 | 1 |
-- | 1 | 精英班 | 2 | John | 11 | 180.00 | 男 | 1 |
-- +------+-----------+----+------+------+--------+--------+--------+
---------------------------------------自关联
-- 通常大分类有小分类这种形式的数据放到一个表中,并且pid指向表的id
CREATE TABLE `areainfo` (
`id` int(10) unsigned NOT NULL,
`name` varchar(32) DEFAULT NULL,
`pid` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
)
--- 查询广东省下的所有地级市
select c.name from areainfo as p inner join areainfo as c
on c.pid=p.id where p.name='广东省';
--- 查询山东省下的所有地级市和县区
-- 注意: [a join b on 条件] 结果是一个`表`********************
select city.name, county.name from areainfo as county join
(areainfo as city join areainfo as province
on city.pid=province.id and province.pid is null)
on city.id=county.pid
where province.name='山东省';
select city.name, county.name from (areainfo as county join
(areainfo as city join areainfo as province
on city.pid=province.id and province.pid is null)
on city.id=county.pid)
where province.name='山东省';
---------------------------------- 子查询
--- 每个SQL包含两部分 主查询 和 子查询
-- 子查询有三种类型
-- 标量子查询: 子查询返回的结果是一个数据(一行一列)
-- 列子查询: 返回的结果是一列(一列多行)
-- 行子查询: 返回的结果是一行(一行多列)
-- 标量子查询: 将子查询的结果当成一个值
-- 查询大于平均年龄的学生
select * from students where age > (select avg(age) from students);
-- 列子查询: 将子查询的结果当成同一属性(列)多个值的的集合
-- 查询班级还存在的学生的名字
select name from students where cls_id in (select id from classes);
-- 行子查询: 将多列数据看成一条数据
-- 查找班级年龄最大,身高最高的学生
select * from students where (height, age)=
(select max(height), max(age) from students);
-- 只有在最大身高、最大年龄刚好是一个人的时候才能查找到数据。
2sql的更多相关文章
- tp3.2sql改变时间格式
tp3.2sql改变时间格式2018-05-10取05-10 $listIn=D('api_article as a')->field('date_format( fabutime,\'%m-% ...
- 2-sql基本操作
sql基本操作 一.Sqlplus常用命令 1.查看oracle数据库的进程 2.查看oracle数据库运行状态 3.显示实例名(数据库名) 4.用sys账户登陆到数据库 5.解锁账户scott,并登 ...
- Ibatis学习总结2--SQL Map XML 配置文件
SQL Map 使用 XML 配置文件统一配置不同的属性,包括 DataSource 的详细配置信息, SQL Map 和其他可选属性,如线程管理等.以下是 SQL Map 配置文件的一个例子: Sq ...
- 2015.4.2-SQL 简单语句(一)
1.创建一个student表 create table student_masen( sno char(4) not null primarykey sname nchar(10) not null ...
- 关于面试总结2-SQL学生表
前言 接着上一篇https://www.cnblogs.com/yoyoketang/p/10065424.html,继续学生表SQL 1.计算每个人的平均成绩, 要求显示字段: 学号,姓名,平均成绩 ...
- 2- SQL语句的强化
查询类型cate_name为 '超极本' 的商品名称.价格 select name,price from goods where cate_name = '超级本'; 显示商品的种类 select c ...
- 廖雪峰Java15JDBC编程-2SQL入门-2insert/select/update/delete
1. INSERT用于向数据库的表中插入1条记录 insert into 表名 (字段1,字段2,...) values (数据1,数据2,数据3...) 示例 -- 如果表存在,就删除 drop t ...
- 廖雪峰Java15JDBC编程-2SQL入门-1SQL介绍
1.SQL:结构化查询语言 Structured Query Language 针对关系数据库设计 各种数据库基本一致 允许用户通过SQL查询数据而不关心数据库底层存储结构 1.1 SQL使用: 可以 ...
- 00 MySQL
1数据库 1.1名词解释 DB:数据库Database,用于存放数据仓库 DBMS:数据库管理系统 DataBase Management System,管理数据库 ta ...
随机推荐
- 【从刷面试题到构建知识体系】Java底层-synchronized锁-2偏向锁篇
上一篇通过构建金字塔结构,来从不同的角度,由浅入深的对synchronized关键字做了介绍, 快速跳转:https://www.cnblogs.com/xyang/p/11631866.html 本 ...
- WARNING: The host 'WeiLei' could not be looked up with resolveip.
[root@WeiLei data]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysq ...
- 【Redis深度历险】那些年Redis的数据结构
[Redis深度历险]那些年Redis的数据结构 Redis端口号6379的来源 Redis的端口号是6379,但这个端口号并不是随机选择的,源于"MERZ",这个单词在手机当中的 ...
- Nmap的一些常用的nse脚本
转自freebuf.com/ 点击跳转 在这篇文章中,我们将研究最著名的渗透工具之一 Nmap 一款标志性的跨平台扫描器.它的原意为Network Mapper(网络映射器),具有相当强大的扫描功能 ...
- [ZJOI2006]碗的叠放
Description 小H有n个碗需要放进橱柜,她希望将他们叠起来放置.你知道每个碗都是规则的圆柱体,并且都是上宽下窄,你已经测量出了每个碗的两个半径及高,请你帮小H找出一种叠放顺序,使得叠放出来的 ...
- NIO 在Tomcat中的应用
对NIO的理解 个人单方面认为,NIO与BIO的最大区别在于主动和被动,使用BIO的方式需要等待被调用方返回数据,很明显此时调用者是被动的. 举个例子 阻塞IO 假设你是一个胆小又害羞的男孩子,你约了 ...
- Ubuntu16.04下nvidia驱动+nvidia-docker+cuda9+cudnn7安装
一.宿主机安装nvidia驱动 打开终端,先删除旧的驱动: sudo apt-get purge nvidia* 禁用自带的 nouveau nvidia驱动 sudo gedit /etc/modp ...
- Net Core Identity 身份验证:注册、登录和注销 (简单示例)
一.前言 一般我们自己的系统都会用自己设置的一套身份验证授权的代码,这次用net core的identity来完成简单的注册.登录和注销. 二.数据库 首先就是创建上下文,我这里简单的建了Users和 ...
- mysql group by使用方法注意
mysql group by使用方法注意 group by 后面只用能用having 不能加 where等域名
- 对于 TCP 三次握手的理解
假设名叫 A 和 B 的两个人要进行通信,那么他们两人之间,首先要确保通信顺畅. 而确保通信顺畅,就要从 3 个维度,确定 8 个能力 3 个维度分别是: 1.人知道(A 知道.B 知道) 2.人(A ...