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 的实现方式还是有差别的.今天我们就从数据库的角度来 ...
随机推荐
- Docker下安装rabbitmq
拉取镜像 docker pull rabbitmq:-management 启动镜像(默认用户名密码),默认guest 用户,密码也是 guest docker run -d --: -p : rab ...
- ORACLE升级PSU&OJVM注意的问题及遇到问题解决思路
[环境介绍] 系统环境:Solaris + Oracle 11R2 + OGG + 脚本定时任务统计信息收集 [背景描述] 基于集团的安全检查,需要对数据库版本进行漏洞扫描,漏洞扫描中存在RBDMS和 ...
- docker学习-----docker可视化portainer
docker的可视化操作界面portainer 1.创建一个挂载区 docker volume create portainer_data 2.安装( docker run -d - ...
- 第六章 接口,lamda表达式与内部类
接口 接口可以包含常量, 且不需要publish static final修饰, 接口的域会自动添加该修饰符. Java建议不要写多余的代码,因此省略修饰符更简洁. 全部都是常量的接口背离了接口的初衷 ...
- ch03 课下作业——缓冲区溢出漏洞实验
一.实验简介: 缓冲区溢出是指程序试图向缓冲区写入超出预分配固定长度数据的情况.这一漏洞可以被恶意用户利用来改变程序的流控制,甚至执行代码的任意片段.这一漏洞的出现是由于数据缓冲器和返回地址的暂时关闭 ...
- pom文件报错org.apache.maven.archiver.mavenarchiver.getmanifest
eclipse导入新的maven项目时,pom.xml第一行报错: org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.mav ...
- 推荐一款中国风React组件
最近看这个中国风的组件在掘金也火了一段时间,看了有几天了,也体验了下,感觉还不错,所以准备来安利下 项目地址:https://github.com/zhui-team/zhui 使用手册请参考:htt ...
- mysql 迁移
背景 这次做oracle数据迁移,也想总结像mysql的数据迁移方式.简单列下吧,因为具体方式网上很多. 方式 可以通过修改mysql.ini的数据文件目录位置方法实现拷贝迁移,此种方式简单 通过备份 ...
- 【原创】运维基础之Docker(1)简介、安装、使用
docker 18.09 官方:https://docs.docker.com/ 一 简介 Docker is a platform for developers and sysadmins to d ...
- Jmeter性能测试之Monitor监控(SSHMon Samples Collector)
前面写的一篇Monitor监控有缺陷, 这篇文章使用Jmeter4.0+的版本, 使用插件SSHMon Samples Collector来做资源监控 1. 官网下载插件: plugins-manag ...