SQL常用语句

来源:互联网 发布:matlab离散数据求积分 编辑:程序博客网 时间:2024/06/16 10:12

SQL常用语句

 

1.简单的查询

查询testtable表中姓名为张三的nickname字段和email字段

select nickname, email from testtable  where name='张三'

 

2.查询结果集合中数据的排列顺序与选择列表中所指定的列明排列顺序相同

 select  nickname,email from  testtable

 

3.更改标题

select var1=nickname,var2=email from testtable

 

4.限制返回的行数

返回前20条或者前20%的行数

select  top 20 * from testtable 

select top 20 percent  *  from testtable

 

 

FROM子句

 

1.FROM子句指定select语句查询及与查询相关的表或视图。在from子句中最多可以指定256个表或视图,他们之间用逗号分隔。

 

select  username,citytable.cityid  from  usertable,citytable  where  usertable.cityid=citytable.cityid

 

 

2.在from子句中可用以下两种格式为表或视图指定别名:

表明   as  别名

表明    别名

select  username,b.cityid  from  usertable   a,citytable    b   where  a.cityid=b.cityid

 

查找usertable表中字段username,要求不包含A 又要包含B的所有字段

select  username  from  usertable  (select username from usertable  where username<>'*A*') as t  where  t.username ='*B*'

 

 

 where子句

 

原创粉丝点击