# 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. 查看weblogic版本号

    通过WebLogic配置文件config.xml,示例如下: # cat config.xml|grep version

  2. Vim使用技巧:特定文件类型关联缩进

    Vim如何打开特定文件类型关联自动缩进呢?答案:将filetype indent on写入你的.vimrc文件中

  3. oldboy s21day10

    #!/usr/bin/env python # -*- coding:utf-8 -*-   # 1.写函数,函数可以支持接收任意数字(位置传参)并将所有数据相加并返回. ''' def func(* ...

  4. DIV浮动层被OCX控件遮蔽解决方案

    在开发中需要在网页中嵌入OCX控件,但是控件嵌入后,总是会出现在网页最顶层,页面中的浮动DIV总是不能正常显示,会被遮蔽掉,那么这里就需要特殊处理一下: OBJECT会遮蔽掉页面内容,但是IFRAME ...

  5. CCPC-Wannafly Winter Camp Day5 (Div2, onsite) Sorting(线段树)

    题目链接 题意 对序列进行三种操作: 1.区间求和. 2.将区间小于等于$x$的数不改变相对顺序的前提下放到$x$左边,用同样规则将比$x$大的放到右边. 3.将区间大于$x$的数不改变相对顺序的前提 ...

  6. jquery.ajax()详解

    jQuery.ajax() 函数详解 traditional 如果你希望使用传统方式来序列化参数,将该属性设为true. 传递数组时, traditional必须为true var arr = []; ...

  7. 【Java编程思想笔记】注解--自定义注解

    文章参考自:https://www.cnblogs.com/xdp-gacl/p/3622275.html 学习网站:how2java.cn 一.自定义注解的创建过程 第一步:(元注解)   使用元注 ...

  8. 帆软报表(finereport)常用函数

    1. SUM SUM(number1,number2,…):求一个指定单元格区域中所有数字之和.Number1,number2,…:1到30个参数或指定单元格区域中所有数字. 注: 函数将直接键入参数 ...

  9. WX支付功能的调用

    1.引入js. 2.微信支付的js.此处有的理解不知对不对,是照着老大的例子整改的~ $('.button').click(function () { var giftId = $('.show'). ...

  10. 使用Pycharm创建一个Django项目

    在使用python写脚本一段时间后,想尝试使用Django来编写一个python项目,现做以下记录备忘: 1.创建项目 如果本地没有安装与所选python版本对应Django版本,pycharm会自动 ...