数据采集中常用的SQL语句

数据采集中常用的SQL语句

   相同的SQL语句运用到不同数据库中会有略微的差别,对字符变量的要求,相关函数的变化,以及语法规则的不同等等,例如:oracle数据库中对字段命名 别名时不需要as 字符,没有month(),year()等时间函数等等,access数据库中在使用inner join执行内部联合时条件需用(),当然还有很多的细微差别,大家可以自己去寻找总结。下面的示例以SQL SERVER为基础编写。

1. 抽取非重复数据

select distinct var1 from tableName;

2. 抽取某个时间段间的数据

select var1,var2 from 数据表 where 字段名 between 时间1 and 时间2;
3. 连接多个变量

select '123'+cast(456 as varchar);

select '123'+cast(456 as varchar)+'789';

4. 用SQL语句找出表名为Table1中的处在ID字段中1-200条记录中Name字段包含w的所有记录

select * from Table1 where id between 1 and 200 and Name like '%w%';

5. 找出拥有超过10名客户的地区的列表

select country from test group by country having count(customerId)>10;
6. 关于取出每个部门工资最高的前三人

select * from table t where 工资 in (select top 3 工资 from table where 部门 = t.部门 order by 工资 desc);  
7. 两个结构完全相同的表a和b,主键为index,使用SQL语句,把a表中存在但在b表中不存在的数据插入的b表中

insert into b select * from a where not exists(select * from b where "index"=a."index");  
8.从一个数据库中的多个数据表提取相关变量

Select table1.var1,table2.var2,table2.var3,

From table1 inner join table2

On tabel1.var1=table2.var1

Inner join table3

On tabel1.var2=table3.var2

(order by ……)

猜你喜欢

转载自ryee.iteye.com/blog/1157570