# 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的基础用法的更多相关文章

  1. SQL server基础知识(表操作、数据约束、多表链接查询)

    SQL server基础知识 一.基础知识 (1).存储结构:数据库->表->数据 (2).管理数据库 增加:create database 数据库名称 删除:drop database ...

  2. elasticsearch安装与基础用法

    来自官网,版本为2.3 注意elasticsearch依赖jdk,2.3依赖jdk7 下载rpm包并安装 wget -c https://download.elastic.co/elasticsear ...

  3. SQL 语句日期用法及函数

    SQL 语句日期用法及函数 --DAY().MONTH().YEAR()——返回指定日期的天数.月数.年数:select day(cl_s_time) as '日' from class  --返回天 ...

  4. SQL 中ROLLUP 用法

    SQL 中ROLLUP 用法 ROLLUP 运算符生成的结果集类似于 CUBE 运算符生成的结果集. 下面是 CUBE 和 ROLLUP 之间的具体区别: CUBE 生成的结果集显示了所选列中值的所有 ...

  5. oracle入坑日记<六>自增列创建和清除(含序列和触发器的基础用法)

    0   前言 用过 SQLserver 和 MySQL 的自增列(auto_increment),然而 Oracle 在建表设置列时却没有自增列. 查阅资料后发现 Oracle 的自增列需要手动编写. ...

  6. SQL语法基础之SELECT

    SQL语法基础之SELECT 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.SELECT查看帮助信息 1>.查看SELECT命令的帮助信息 mysql> ? SEL ...

  7. SQL语法基础之UPDATE语句

    SQL语法基础之UPDATE语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看UPDATE语句的帮助信息 1>.查看UPDATE的帮助信息 mysql> ? ...

  8. Spring中JdbcTemplate的基础用法

    Spring中JdbcTemplate的基础用法 1.在DAO中使用JdbcTemplate 一般都是在DAO类中使用JdbcTimplate,在XML配置文件中配置好后,可以在DAO中注入即可. 在 ...

  9. SQL——语法基础篇(上)

    用数据库的方式思考SQL是如何执行的 虽然 SQL 是声明式语言,我们可以像使用英语一样使用它,不过在 RDBMS(关系型数据库管理系统)中,SQL 的实现方式还是有差别的.今天我们就从数据库的角度来 ...

随机推荐

  1. js常用数据类型(Number,String,undefined,boolean) 引用类型( function,object,null ),其他数据类型( 数组Array,时间Date,正则RegExp ),数组与对象的使用

    js常用数据类型 数字类型 | 字符串类型 | 未定义类型 | 布尔类型 typeof()函数查看变量类型 数字类型  Number var a1 = 10; var a2 = 3.66; conso ...

  2. mysql MHA架构搭建过程

    [环境介绍] 系统环境:Red Hat Enterprise Linux 7 + 5.7.18 + MHA version 0.57 系统 IP 主机名 备注 版本 xx系统 192.168.142. ...

  3. Geometric regularity criterion for NSE: the cross product of velocity and vorticity 4: $u\cdot \om$

    在 [Berselli, Luigi C.; Córdoba, Diego. On the regularity of the solutions to the 3D Navier-Stokes eq ...

  4. SCI,EI,ISTP

    SCI:   Science Citation Index EI:     The Engineering Index ISTP:  Index to Scientific & Technic ...

  5. 学习熟悉箭头函数, 类, 模板字面量, let和const声明

    箭头函数:https://blog.csdn.net/qq_30100043/article/details/53396517 类:https://blog.csdn.net/pcaxb/articl ...

  6. mysl 常用函数 union all if ifnull exists case when

    1.union all UNION 操作符用于合并两个或多个 SELECT 语句的结果集.请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 S ...

  7. C#基础零碎知识点摘录

    1.类分为静态类个非静态类(实例类) 静态类不能创建对象,使用方法时,直接类名.方法名(),常用的静态类有Console类 实例类:创建对象时通过对象调用类的方法 2.当我们声明一个类成员为静态时,意 ...

  8. 【easy】226. Invert Binary Tree 反转二叉树

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  9. Linux下安装python的gmpy2库及遇到无法定位软件包的解决办法

    gmpy2需要gmp.h &mpfr.h &mpc.h 安装命令: sudo apt-get install libmpfr-dev libmpc-dev  成功之后再输入安装命令: ...

  10. extensible_index

    create user ex identified by oracle; grant Resource to ex;grant connect to ex;grant create view to e ...