Sql语句积累

 --查询
select  employeeid,firstname,lastname    from test

 --插入
insert into test(lastname,firstname)values('yan','xu') 

--更新,修改
update test
set firstname='Xu',title='the center of computer',region='XA',country='CH',city='XI AN'
where employeeid=10

 --平均值 
select avg(employeeid) as 平均值  from test

--将小写的字符数据转化为大写,lower相反

select upper(lastname)as mingzifrom test
where employeeid='1'

select getdate()     --用getdate()函数获取当前系统的时间

--从getdate()返回的日期中提取月份数,可以用同样的方法提取年,日--

select datepart(month,getdate()) as 'month number'

--复制表--

select * into test
from Northwind.dbo.Categories

--取最大值--

select max(id) as Max_ID from customer

--返回employees表中首列的名称

select col_name(object_ID('employees'),1)

--取出列并重命列名的两种方法(用as和不用as)
select au_lname "Author last name" from authors
select au_lname as 'Author last name' from authors

--查询结果执行运算
select price "Original price",price*2 "New price" from titles

select price,ytd_sales,price*ytd_sales "Total revenue" from titles

--使用连接运算符(它看起来像个加号)来连接两个字符型字段
select au_fname+' '+au_lname "author name" from authors

select count(*) "sum" from authors --相同于select count(au_id)"sum" from authors


--找出某个条件在某个区间上的数据
select royalty from titles where royalty  between 12 and 20

--以下两种写法得到的结果一样,满足其中一个条件
select royalty from titles where royalty=14 or royalty=16
select royalty from titles where royalty in(14,16)

--在非字符串类型的字段后面加字符串时需要先将给字段类型转换为字符串类型,如下:
select convert(char(8),price)+'US Dollars' from titles
select 'The price is'+convert(char(8),price) from titles

--取出名字首字母在A-F间的名字
select au_lname "Author name" from authors where au_lname like '[A-F]%'

--取出名字首字母不在A-F间的名字

select au_lname "Author name" from authors where au_lname like '[^A-F]%'

--以CGH开头的所有名字都将被返回

select au_lname from authors where au_lname like '[CGH]%'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值