SQL server 数据库基础语句 查询语句

这一章要学习查询语句

我看car这一数据 我们就开始打上

select  *from car

条件修改

update 表名 set 列名1=值1 where 列名2=值2   //当列名2=值2时  修改 列名1 的一个值 = 值1    

                                                                      列名2可以写多个 之间用and链接   列名=值  不用双等号  可以>=  <=  >  <

例如改code列中  c014   修改  time列   的时间为 2015-1-1    code是唯一列

  update car set time='2015-1-1' where code='c014'

意思是当code=c014这一条拽出来  把time这一列改为2015-1-1

 当oil=7.4并且powers=188    修改time=2010-1-1

update car set time='2010-1-1' where oil=7.4 and powers=188

   因为有两行  oil =7.4 power=188的   所以该两行

  删除条件

delete from 表名 where 列名=值

我要删除 c016的这一条数据

语句是  delete  from car where code='c016'

条件查询

select 列 from 表名    //多列用逗号隔开

例如  我要查询name这一列 

     select name from car      //那就把name这一列显示出来

查oil>7.4    显示name  oil  

select name,oil from car where oil>7.4

模糊查询

select 列名 from 表名  where 列  like '%含有的值%'        %在这里代表的是通配符      *代表所有列

我要查询  带有皇冠的信息

 select *from car where name like'%皇冠%      //皇冠%  是以皇冠开头的      %皇冠 是以皇冠结尾

'

排序查询

select * from 表名 order by 列名 asc  升序 

select * from 表名 order by 列名 desc 降序

例如: 把car 表用价格从小到大排序

selecr *from car order by price asc

去重查询

select distinct 列名 from 表名

例如:只查油耗oil这一列

 select distinct oil from car

分组查询

 select 列名 from表名 group by 列名       前后列名一致   否则电脑不知道怎么分组

例如:把oil分组

select oil from car group by oi            显示的是 和上面的一样  但是以后学习会知道  每一种有多少个

l

子查询(有关系表car和brand表      brand表约束car表)

将查询的语句当做只来使用

例如:  用brand表 宝马5系  找car表的信息

select brand_code from brand where brand_name='宝马5系'

显示的值是   b004

查car中的b004

 select *from car where brand='b004'

把brand='b004'  当做一种值来用

两句合为一句

 select *from car where brand=(select brand_code from brand where brand_name='宝马5系')

 

原文地址:https://www.cnblogs.com/zhangwei99com/p/6536756.html