sql的基础用法
# sql 对大小写不敏感 # 查询表中的所有信息
select * from `Customers`;
# 查询指定字段 CustomerName,Country
select CustomerName,Country from `Customers`
# distinct 去重复
select * distinct Country from `Customers`
# 客户的国家数量
select COUNT(DISTINCT Country) from `Customers`
#where 语句
select * from `Customers` where Country = 'Mexico'
#= 等于 <> 不等于 > 大于 < 小于 >= 大于等于 <= 小于等于 between 在某个范围内 like 搜索某种模式 IN 为列指定多个可能的值
select * from `Customers` where `CustomerID`=1
#<> 不等于1
select * from `Customers` where CustomerID <> 1
#>
select * from `Customers` where CustomerID > 3
#<
select * from `Customers` where CustomerID < 3
#>=
select * from `Customers` where CustomerID >= 3
#<=
select * from `Customers` where CustomerID <= 3
#between and 3~5
select * from `Customers` where CustomerID between 3 and 5
#like
select * from `Customers` where `City` like '%n%'
select * from `Customers` where `City` like '%n'
#and
select * from `Customers` where `Country`='Germany' and City = 'Berlin'
#or
select * from `Customers` where `Country`='Germany' or City = 'London'
#Not
select * from `Customers` where not Country = 'Germany'
#结合 and or
select * from `Customers` where Country = 'Germany' and (City='Berlin' OR City='München')
#order by
select * from `Customers` order by Country;
#order by desc 降序排列
select * from `Customers` order by Country desc;
#order by 多列
select * from `Customers` order by Country,CustomerName;
#order by 多列实例2
select * from `Customers` ORDER BY Country ASC,CustomerName DESC;
#insert into 实例 插入数据
insert into `Customers`(CustomerName,ContactName,Address,City,PostalCode,Country)
values ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway')
#仅在指定的列中插入数据
insert into `Customers`(CustomerName,City,Country) values ('Cardinal','Stavanger','Norway')
#NULL 空值 如何测试NULL值
select CustomerName,ContactName,Address from `Customers` where Address is null
#is not null
select * from `Customers` where Address is not null
#更新表中的记录 update
# update table_name set column1 = value1,column2 = value2,.... where condition
update `Customers` set ContactName = 'Alfred Schmidt',City='Frankfurt999999' where CustomerID =1
#更新多个记录数据
update Customers set ContactName = 'Juan' where Country='Mexico'
#删除数据 delete
# delete from table_name where condition
delete from Customers where CustomerName='Alfreds Futterkiste'
#删除所有的数据
#delete from table_name
#或者
# delete * from table_name ###############################################SQL 高级##########################
#前4条数据
select * from `Customers` limit 4
#like 搜索 关键字
# % 百分号标识零个,一个或多个字符
# _ 下划线标识单个字符
select * from `Customers` where CustomerName like 'a%'
select * from Customers where CustomerName like '%or%'
# _ 单个字符
#选择客户名称在第二位具有r的所有客户
select * from Customers where CustomerName like '_r%'
#以a开头 至少有三个字符
select * from Customers where CustomerName like 'a_%_%'
#以a开头并且以o结尾
select * from Customers where CustomerName like 'a%o'
# 不以a开头的所有客户
select * from Customers where CustomerName not like 'a%'
# 不以bsp 开头
select * from Customers where CustomerName like '[!bsp]%'
#IN 运算符允许您在where子句中指定多个值 IN运算符是多个or条件的缩写
select * from Customers where Country in ('Germany', 'France', 'UK')
# not in
select * from Customers where Country not in ('Germany', 'France', 'UK')
#选取来自同一国家的所有客户作为供应商:
select * from `Customers` where Country in (select Country from Suppliers)
#between and
select * from Products where Price between 10 and 20
#not betwee and
select * from Products where price not between 10 and 20
#价格在10到20之间但CategoryID不是1、2或3的所有产品
select * from Products where (Price Between 10 and 20) and not CategoryID in (1,2,3)
#时间段内的所有订单
select * from Orders WHERE OrderDate BETWEEN '#07/04/1996#' AND '#07/09/1996#' #inner join on 交集
select Orders.`OrderID`,`Customers`.`CustomerName`,`Orders`.`OrderDate` from `Orders`
inner join `Customers` on Orders.`CustomerID`=Customers.`CustomerID`
#inner join 关键字(内部连接) 内部链接inner join 关键字选择两个表中具有匹配值的记录
#返回所有下订单的客户
select Customers.`CustomerName`,Orders.`OrderID` from Customers
inner join `Orders`
on Customers.`CustomerID` = Orders.`CustomerID`
order by Customers.`CustomerName` #查询出医院下面的科室
select `biz_hospital`.`name`,`biz_hospital`.`hospital_id`,`biz_hospital`.`city`,`biz_hospital`.`level`,
`biz_room`.`room_id`,`biz_room`.`name` room_name from `biz_hospital`
inner join `biz_room` on `biz_hospital`.`hospital_id` = `biz_room`.`hospital_id` and `biz_hospital`.`status` =1 and `biz_room`.`status` =1 #查询出来医院下面所有科室下面的所有医生
select `biz_hospital`.`name` hospital_name,`biz_hospital`.`city`,`biz_hospital`.`level`,`biz_room`.`room_id`,`biz_room`.`name` room_name,
`biz_doctor`.`name` doctor_name
from ((`biz_hospital` inner join `biz_room` on `biz_hospital`.`hospital_id` = `biz_room`.`hospital_id` and `biz_hospital`.`status` =1 and `biz_room`.`status`=1 )
inner join `biz_doctor` on `biz_room`.`room_id` = `biz_doctor`.`room_id` and `biz_doctor`.`status`=1) #有开放科室的医院
select `biz_hospital`.`hospital_id`,`biz_hospital`.`name` hospital_name,`biz_hospital`.`city`,`biz_hospital`.`level`
,`biz_open_room`.`room_name`
sql的基础用法的更多相关文章
- SQL server基础知识(表操作、数据约束、多表链接查询)
SQL server基础知识 一.基础知识 (1).存储结构:数据库->表->数据 (2).管理数据库 增加:create database 数据库名称 删除:drop database ...
- elasticsearch安装与基础用法
来自官网,版本为2.3 注意elasticsearch依赖jdk,2.3依赖jdk7 下载rpm包并安装 wget -c https://download.elastic.co/elasticsear ...
- SQL 语句日期用法及函数
SQL 语句日期用法及函数 --DAY().MONTH().YEAR()——返回指定日期的天数.月数.年数:select day(cl_s_time) as '日' from class --返回天 ...
- SQL 中ROLLUP 用法
SQL 中ROLLUP 用法 ROLLUP 运算符生成的结果集类似于 CUBE 运算符生成的结果集. 下面是 CUBE 和 ROLLUP 之间的具体区别: CUBE 生成的结果集显示了所选列中值的所有 ...
- oracle入坑日记<六>自增列创建和清除(含序列和触发器的基础用法)
0 前言 用过 SQLserver 和 MySQL 的自增列(auto_increment),然而 Oracle 在建表设置列时却没有自增列. 查阅资料后发现 Oracle 的自增列需要手动编写. ...
- SQL语法基础之SELECT
SQL语法基础之SELECT 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.SELECT查看帮助信息 1>.查看SELECT命令的帮助信息 mysql> ? SEL ...
- SQL语法基础之UPDATE语句
SQL语法基础之UPDATE语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看UPDATE语句的帮助信息 1>.查看UPDATE的帮助信息 mysql> ? ...
- Spring中JdbcTemplate的基础用法
Spring中JdbcTemplate的基础用法 1.在DAO中使用JdbcTemplate 一般都是在DAO类中使用JdbcTimplate,在XML配置文件中配置好后,可以在DAO中注入即可. 在 ...
- SQL——语法基础篇(上)
用数据库的方式思考SQL是如何执行的 虽然 SQL 是声明式语言,我们可以像使用英语一样使用它,不过在 RDBMS(关系型数据库管理系统)中,SQL 的实现方式还是有差别的.今天我们就从数据库的角度来 ...
随机推荐
- 解决radio、select表单返回时,再次选择失效
应用场景:我们在选择好radio跟select之后提交表单,返回历史记录时,再次选择,提交表单,发现提交的是上次表单选择的 解决办法:我们可以一进页面就给radio跟select的选项重置掉,因为,返 ...
- blinker库
参考 Blinker Documentation Blinker 是一个基于Python的强大的信号库,它既支持简单的对象到对象通信,也支持针对多个对象进行组播.Flask的信号机制就是基于它建立的. ...
- fiddler主要图标说明
主要图标含义说明: 正在将请求数据发往服务器 正在从服务器下载返回数据 请求过程中暂停 返回过程中暂停 请求中使用了HTTP HEAD方法; 返回中应该没有body内容 请求中使用了HTTP CONN ...
- VMware的NAT网络模式
参考链接:https://www.cnblogs.com/linjiaxin/p/6476480.html 图例:
- jenkins结合gitlab实现提交代码自动构建
jenkins可以说是现在非常流行的一个继续集成工具,几乎所有的公司都在用,并且它也基本是devops的连接者,是一个比较核心的工具. 主要记录以下两个: 利用jenkins和gitlab的webho ...
- [转]PuTTY字体颜色设置
转载于 https://blog.csdn.net/cyd_shuihan/article/details/77836290 用putty登录Linux,默认配色方案看不清,我们可以自己设置新的字体大 ...
- Python 爬虫 某迅漫画 selemiun+plantomJS
目标站点需求分析 爬取某迅漫画到本地 涉及的库 selenium, PhantomJS time,urllib.request,os,random 模拟滑动窗口,获取完整网页 保存到文件中 获取本地h ...
- MySQL入门命令
SQL(Structured Query Language) SQL是结构化查询语言,是一种用来操作RDBMS的数据库语言,当前关系型数据库都支持使用SQL语言进行操作,也就是说可以通过 SQL 操作 ...
- IntelliJ IDEA,酷炫插件系列,提高你的工作效率
今天介绍一下IDEA的一些炫酷的插件,IDEA强大的插件库,不仅能给我们带来一些开发的便捷,还能体现我们的与众不同. 1.插件的安装 打开setting文件选择Plugins选项 Ctrl + Alt ...
- 模块度Q
模块度:也称模块化度量值,是目前常用的一种衡量网络社区结构强度的方法. 常用语衡量一个社区划分结果的优劣:一个理想化的社区划分应该对应着社区内部节点间相似度尽可能的高,同时社区外部节点间的相异度尽可能 ...