SQL常用语句

表一table中有字段id,name,sex,birthday,enterdate;
表二table2中有字段id,QQ,mail,website,score

一、数据的查询


1、查询表中所有记录
   select * from table

2、查询表中所有记录,但只要name和sex字段
   select name,sex from table

3、修改表的字段名(即列标题)并将记录的这两个字段输出
   select '姓名'=name,'性别'=sex from table
   或
   select name as 姓名,sex as 性别 from table
   以上两条语句的执行结果相同

4、将表中出生日期在1986年9月22日后的数据输出
   select * from table where birthday > '1986-9-22'

5、查询表中姓名为'罗'的人
   select * from table where name like '罗%'

6、使用关键字between查询出生日期在1986年9月22日--2006年4月10之间的数据
   select * from table where birthday between '1986-9-22'and '2006-4-10''

7、使用关键字in查询出生日期为(1986-9-22,1987-5-3,1988-1-15)的数据
   select * from table where birthday in (1986-9-22,1987-5-3,1988-1-15)

8、将出生日期不重复的记录查询出来
   select distinct birthday from table

9、按出生日期进行升序和降序排列
   升序:select * from table order by birthday asc
   降序:select * from table order by birthday desc

10、关联的使用,将表一的ID与表二的ID进行关联,在where后的为关联条件
   select
   table.id,table.name,table.sex,table2.qq,table2.mail,table2.website
   from table,table2
   where table.id=table2.id


11、group by语句的使用,统计每个出生日期的总人数
   select birthday,count(*) as 人数 from table group by birthday

12、group中使用having,如果要知道具体某个出生日期出生的人数就用如下语句,以下语句是统计出生      日期在1986年的总人数
   select birthday,count(*) as 人数 from table group by birthday
   having birthday between '1986-1-1' and '1986-12-30'


13、子查询的使用,在使用子查询时子查询语句要用圆括号括做,以此来保证子查询首先被执行,以下是根据表table中姓名为'罗兵'的记录查  找出该条记录的ID列的值,然后根据ID值再查询表table2的QQ、MAIL、WEBSITE等内容
   select * from table2 where id=(select id from table where name='罗兵')

14、在使用子查询时候通常把子查询返回的结果作为IN关键字例举值来使用
   select * from table2 where id in(select id from table where name='罗兵' or name='张三')


二、数据的插入、修改、删除

1、使用insert语句插入数据
   insert into table(列) values (值)

2、使用select into语句插入数据
   select (列) into table2 from table
   比如将表table复制为table2(复制时是完全复制,包括表结构和记录)
   select * into table2 from table
   如果表2已经建好,只复制表1中的记录到表2中(不复制结构),则语法如下  
   insert into table2 select * from table

3、使用update语句修改数据
   update table set 列1=值1,列2=值2 where 条件

4、使用delete语句删除数据
   delete from table where 条件


三、SQL函数
   

 聚合函数(共5个)
1、计算字段的平均值函数AVG()
   将表2中score字段的平均值命名为score1
   select avg(score) 'score1' from table2

2、统计表中记录的函数count()
   计算表中总的记录数,如有200条路那就将返回200
   select count(*) from table
   如果要计算表中姓名为'罗兵'的记录数
   select count(name) from table where name='罗兵'
   如果要知道表中不同名字的人数就用如下语句
   select count(distinct name) from table

3、计算字段值的函数和SUM()
   求出字段SCORE的和
   select sum(score) from table

4、返回最大值或最小值函数MAX()和MIN()
   返回字段SCORE为最大值的记录
   select max(score) from table  
   如果返回字段SCORE为最小值的记录则用如下语句
   select min(score) from table
   

    字符串函数

1、rtrim()和ltrim(),函数ltrim()为去掉字符串前的所有空格;函数rtrim()为去掉字符串尾部的所有空格
   select rtrim(name) from table
   这个例子中,如果一个人的名字尾部有多余的空格,那么多余的空格将会从查询结果中被删去
   如果要同时删去前面的空格和后面的空格,则语句如下
   select ltrim(rtrim(name)) from table

2、按照发音来匹配字符串的函数,函数soundex()给一个字符串分配一个音标码,函数difference()按照发音比较两个字符串
   select site_name from table
   where difference(site_namem,'microsoft'>3)
   该语句的意思是通过使用函数difference()来取得名字的发音与microsoft非常相似的站点,返回一个0到4之间的数字,如果函数返回为4,   则发音非常相进,如果返回0,则发音相差很大。
   select site_name 'site name',soundex(site_name) 'sounds like'

 日期处理函数

1、获取当前的日期和时间
   select getdate()

2、转换日期和时间
   如果要显示的时间包括毫秒
   select convert(varchar(30),getdate(),9)

3、抽取日期和时间datepart()
   select site_name 'site name',datepart(mm,site_entrydate) 'month posted' from site_directory
   函数DATENAME()来抽取一个星期中的某一天
   select site_name 'site name',
   datename(dw,site_entrdate)+'-'+datename(mm,site_entrydate)
   'day and month posted' from site_directory

4、返回日期和时间范围
   select * from table where entrydate>=&apos;12/25/2000&apos; and entrydate<&apos;12/26/2000&apos;

5、比较日期和时间
   select enterdate &apos;入学时间&apos;,datediff(dd,enterdate,getdate()) &apos;入学天数&apos; from students

select A.字段1, A.字段2,B.字段1 from A left join b on b.字段1=A.字段1(left join b就是连接b表,把左表中的记录全列出,右表中与左表有想同的就列)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值