SQL新手养成日记
2017-06-26 23:14
363 查看
基本的数据库
databases
软件名称 management studio
数据的查询 编辑 添加
ER图
dbo.EDM
table和table之间有关系
叫做关系型数据库管理系统(RDBMS)
SQL 叫做 C扣
关系型数据库的基本要素是二维表
行,列
主键(PK),外键(FK)
主键不能为空
什么事T SQL结构型的查询语言
用处:通过数据来回答商业运营中的实际问题从而帮助公司企业决策者做出更
好的决策
-----------------------
SELECT(DISTINCT)
FROM
WHERE
GROUP BY
HAVING
UNION
ORDER BY
select<table fields list>
*表示表格中的所有数据
尽量少用*
desc倒序排列 asc正序排列(可以省略)。
可以用select Top 100 * from [prodcution].[product]也可以不用加框框。
选中一部分select productID,Name,ProductNumber,Color,Size,ListPrice
from prodcution.product
orde
4000
r by Listprice desc,Name
解释:进行两次排列,一次按照Listprice倒序排列,一次按照Name正序排列。
select productID,Name,ProductNumber,Color,Size,ListPrice from
prodcution.product
order by 2
解释:按照我们选出的第二个Name进行正序的排列
--------------------------
isnull
null代表空值,但是我们不想在数据中呈现
方法:
select productID,Name,ProductNumber,isnull(Color,''),isnull
(Size,''),ListPrice from prodcution.product
-------------------------------
as
给自己表头FK重命名
select productID,Name,ProductNumber,isnull(Color,'') as Color,
isnull(Size,'') Size123,ListPrice
from prodcution.product
ps:备注方法:- -东西*************
select BusinessEntityID,rate from HumanResources.EmployeePayHistory
select BusinessEntityID,
rate*40*52 as AnnualSalary,
round(rate*40*52,1) as AnnualSalary,
round(rate*40*52,0) as AnnualSalary
from HumanResources.EmployeePayHistory
备注 1,0表示 四舍五入的位置 ,但是所有的依旧显示两位小数点。
------------------------
where
select * from sale.salesorderheader
where salespersonID=275
select * from sale.salesorderheader
where TotalDue>5000
select * from sale.salesorderheader
where SalesOrderNumber='so43670'
ps:int整型,不用加‘’,String字符型,需要加‘’。
select * from sale.salesorderheader
where salespersonID=275 and TotalDue>5000
select * from sale.salesorderheader
where salespersonID=275 and TotalDue>5000 and Orderdate between'2005
-08-01' and '1/1/2006'
ps:这两种时间的表示方式都是可以的
select * from sale.salesorderheader
where salespersonID=275 and TotalDue>5000 and Orderdate >= '2005-08-
01' and Orderdate < '1/1/2006'
对于字符串的引用
select * from sale.salesorderheader
where name = 'Mountain-100 Silver,38'
类似于超级查找的方式
select * from sale.salesorderheader
where name like '%Mountain%' 这个前后%,表示mountain前面也可以有东西
,后面也可以有东西。
搜索mountain前面没有东西的模糊查找
select * from sale.salesorderheader
where name like 'Mountain%'
-的使用
select * from sale.salesorderheader
where name like '_ountain%'
表示_里面我不知道是什么单词了。
------------------------------------
in的使用
select * from Producrion.product
where size in('60','61','62')
选择size里面 是60,61,62东西的那个字符。
为什么用字符呢,因为这里的size格式是char 具体问题 具体分析。
----------------
not in
----------------
distinct只选择一次。
进行快速的查询
where size not in('60')
-----------------
select * from Producrion.product
where size is null
select * from Producrion.product
where size is not null***经常使用。
select * from Producrion.product
where color = 'white'or color='black'
select * from Producrion.product
where color = 'white'and color='black'
select * from Producrion.product
where (SalesPersonID=275 or SalesPersonID=278) and TotalDue>5000
databases
软件名称 management studio
数据的查询 编辑 添加
ER图
dbo.EDM
table和table之间有关系
叫做关系型数据库管理系统(RDBMS)
SQL 叫做 C扣
关系型数据库的基本要素是二维表
行,列
主键(PK),外键(FK)
主键不能为空
什么事T SQL结构型的查询语言
用处:通过数据来回答商业运营中的实际问题从而帮助公司企业决策者做出更
好的决策
-----------------------
SELECT(DISTINCT)
FROM
WHERE
GROUP BY
HAVING
UNION
ORDER BY
select<table fields list>
*表示表格中的所有数据
尽量少用*
desc倒序排列 asc正序排列(可以省略)。
可以用select Top 100 * from [prodcution].[product]也可以不用加框框。
选中一部分select productID,Name,ProductNumber,Color,Size,ListPrice
from prodcution.product
orde
4000
r by Listprice desc,Name
解释:进行两次排列,一次按照Listprice倒序排列,一次按照Name正序排列。
select productID,Name,ProductNumber,Color,Size,ListPrice from
prodcution.product
order by 2
解释:按照我们选出的第二个Name进行正序的排列
--------------------------
isnull
null代表空值,但是我们不想在数据中呈现
方法:
select productID,Name,ProductNumber,isnull(Color,''),isnull
(Size,''),ListPrice from prodcution.product
-------------------------------
as
给自己表头FK重命名
select productID,Name,ProductNumber,isnull(Color,'') as Color,
isnull(Size,'') Size123,ListPrice
from prodcution.product
ps:备注方法:- -东西*************
select BusinessEntityID,rate from HumanResources.EmployeePayHistory
select BusinessEntityID,
rate*40*52 as AnnualSalary,
round(rate*40*52,1) as AnnualSalary,
round(rate*40*52,0) as AnnualSalary
from HumanResources.EmployeePayHistory
备注 1,0表示 四舍五入的位置 ,但是所有的依旧显示两位小数点。
------------------------
where
select * from sale.salesorderheader
where salespersonID=275
select * from sale.salesorderheader
where TotalDue>5000
select * from sale.salesorderheader
where SalesOrderNumber='so43670'
ps:int整型,不用加‘’,String字符型,需要加‘’。
select * from sale.salesorderheader
where salespersonID=275 and TotalDue>5000
select * from sale.salesorderheader
where salespersonID=275 and TotalDue>5000 and Orderdate between'2005
-08-01' and '1/1/2006'
ps:这两种时间的表示方式都是可以的
select * from sale.salesorderheader
where salespersonID=275 and TotalDue>5000 and Orderdate >= '2005-08-
01' and Orderdate < '1/1/2006'
对于字符串的引用
select * from sale.salesorderheader
where name = 'Mountain-100 Silver,38'
类似于超级查找的方式
select * from sale.salesorderheader
where name like '%Mountain%' 这个前后%,表示mountain前面也可以有东西
,后面也可以有东西。
搜索mountain前面没有东西的模糊查找
select * from sale.salesorderheader
where name like 'Mountain%'
-的使用
select * from sale.salesorderheader
where name like '_ountain%'
表示_里面我不知道是什么单词了。
------------------------------------
in的使用
select * from Producrion.product
where size in('60','61','62')
选择size里面 是60,61,62东西的那个字符。
为什么用字符呢,因为这里的size格式是char 具体问题 具体分析。
----------------
not in
----------------
distinct只选择一次。
进行快速的查询
where size not in('60')
-----------------
select * from Producrion.product
where size is null
select * from Producrion.product
where size is not null***经常使用。
select * from Producrion.product
where color = 'white'or color='black'
select * from Producrion.product
where color = 'white'and color='black'
select * from Producrion.product
where (SalesPersonID=275 or SalesPersonID=278) and TotalDue>5000
相关文章推荐
- ERP新手养成日记
- PCB新手养成日记
- 要养成良好的书写Sql的习惯 ---------转自wanghangzhou1984
- SQL语句日记:IF语句注意事项
- 养成好的SQL习惯
- SQL调优日记之发挥SQL性能与你的写法有关--对比三种方式实现相同功能
- LINQ to SQL之Step by Step及新手问题小结
- sql日记(相关子查询,动态交叉表篇)
- sql 日记
- 养成良好SQL书写习惯
- php大力力 [001节]2015-08-21.php在百度文库的几个基础教程新手上路日记 大力力php 大力同学 2015-08-21 15:28
- 高效能养成日记_周回顾_20130107-13
- SQL 养成一个好习惯是一笔财富
- SQL传送事务日记
- 一只程序猿的养成日记 第一章 第十一节 编写一个函数实现n^k,使用递归实现
- SQL实战日记--数据库文件还原问题排查
- SQL养成一个好习惯是一笔财富
- 要养成良好的书写Sql的习惯 .
- 要养成良好的书写Sql的习惯
- c#学习日记01--新手上路