数据库经典语句(2)

1、复制表(只复制结构,源表名:a 新表名:b)

  法一:select * into b from a where 1<>1

  法二:select top 0 * into b from a

  2、拷贝表(拷贝数据,源表名:a 目标表名:b)

insert into b(a, b, c) select d,e,f from b;

  3、跨数据库之间表的拷贝(具体数据使用绝对路径)

insert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where 条件

  例子:..from b in '"&Server.MapPath(".")&"data.mdb" &"' where..

  4、子查询(表名1:a 表名2:b)

select a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3)

  5、显示文章、提交人和最后回复时间

select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b

        6、外连接查询(表名1:a 表名2:b)

select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

  7、在线视图查询(表名1:a )

select * from (SELECT a,b,c FROM a) T where t.a > 1;

  8、between的用法,between限制查询数据范围时包括了边界值,not between不包括

select * from table1 where time between time1 and time2

select a,b,c, from table1 where a not between 数值1 and 数值2

  9、in 的使用方法

select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)

  10、两张关联表,删除主表中已经在副表中没有的信息

delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )

  11、四表联查问题:

select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....

  12、日程安排提前五分钟提醒

SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5

  13、一条sql 语句搞定数据库分页

select top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段

  14、前10条记录

select top 10 * form table1 where 范围

  15、选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)

select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)

  16、包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表

(select a from tableA ) except (select a from tableB) except (select a from tableC)

  17、随机取出10条数据

select top 10 * from tablename order by newid()

  18、随机选择记录

select newid()

  19、删除重复记录

Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)

  20、列出数据库里所有的表名

select name from sysobjects where type='U'

        21、列出表里的所有的

select name from syscolumns where id=object_id('TableName')

+(1)数据记录筛选:
sql="select*from数据表where字段名=字段值orderby字段名[desc]"
sql="select*from数据表where字段名like'%字段值%'orderby字段名[desc]"
sql="select top10* from数据表where字段名order by字段名[desc]"
sql="select*from数据表where字段名in('值1','值2','值3')"
sql="select*from数据表where字段名between值1and值2"
(2)更新数据记录:
sql="update数据表set字段名=字段值where条件表达式"
sql="update数据表set字段1=值1,字段2=值2……字段n=值n          
    where条件表达式"
(3)删除数据记录:
sql="delete   from数据表where条件表达式"
sql="delete   from数据表"(将数据表所有记录删除)
(4)添加数据记录:
sql="insert   into数据表(字段1,字段2,字段3…)   values(值1,值2,值3…)"
sql="insert   into目标数据表
Select   *   from源数据表"(把源数据表的记录添加到目标数据表)
(5)数据记录统计函数:
AVG(字段名)得出一个表格栏平均值
COUNT(*|字段名)对数据行数的统计或对某一栏有值的数据行数统计
MAX(字段名)取得一个表格栏最大的值
MIN(字段名)取得一个表格栏最小的值
SUM(字段名)把数据栏的值相加
引用以上函数的方法:
sql="select   sum(字段名)   as别名from数据表   where条件表达式"
setrs=conn.excute(sql)
用rs("别名")获取统的计值,其它函数运用同上。
(5)数据表的建立和删除:
CREATE   TABLE数据表名称(字段1类型1(长度),字段2类型2(长度)……)
例:CREATE   TABLE   tab01 (name   varchar (50), datetime   defaultnow ())
DROPTABLE数据表名称(永久性删除一个数据表)
4.记录集对象的方法:
rs.move next将记录指针从当前的位置向下移一行
rs.move previous将记录指针从当前的位置向上移一行
rs.move first将记录指针移到数据表第一行
rs.move last将记录指针移到数据表最后一行
rs.absoluteposition=N将记录指针移到数据表第N行
rs.absolutepage=N将记录指针移到第N页的第一行
rs.pagesize=N设置每页为N条记录
rs.pagecount根据pagesize的设置返回总页数
rs.recordcount返回记录总数
rs.bof返回记录指针是否超出数据表首端,true表示是,false为否
rs.eof返回记录指针是否超出数据表末端,true表示是,false为否
rs.delete删除当前记录,但记录指针不会向下移动
rs.add new添加记录到数据表末端
rs.update更新数据表记录

SQL语句的添加、删除、修改虽然有如下很多种方法,但在使用过程中还是不够用,不知是否有高手把更多灵活的使用方法贡献出来?

添加、删除、修改使用db.Execute(Sql)命令执行操作
╔----------------╗
☆ 数据记录筛选 ☆
╚----------------╝
注意:单双引号的用法可能有误(没有测试)

Sql = "Select Distinct 字段名 From 数据表"
Distinct函数,查询数据库存表内不重复的记录

Sql = "Select Count(*) From 数据表 where 字段名1>#18:0:0# and 字段名1< #19:00# "
count函数,查询数库表内有多少条记录,“字段名1”是指同一字段
例:
set rs=conn.execute("select count(id) as idnum from news")
response.write rs("idnum")

sql="select * from 数据表 where 字段名 between 值1 and 值2"
Sql="select * from 数据表 where 字段名 between #2003-8-10# and #2003-8-12#"
在日期类数值为2003-8-10 19:55:08 的字段里查找2003-8-10至2003-8-12的所有记录,而不管是几点几分。

select * from tb_name where datetime between #2003-8-10# and #2003-8-12#
字段里面的数据格式为:2003-8-10 19:55:08,通过sql查出2003-8-10至2003-8-12的所有纪录,而不管是几点几分。

Sql="select * from 数据表 where 字段名=字段值 order by 字段名 [desc]"

Sql="select * from 数据表 where 字段名 like '%字段值%' order by 字段名 [desc]"
模糊查询

Sql="select top 10 * from 数据表 where 字段名 order by 字段名 [desc]"
查找数据库中前10记录

Sql="select top n * form 数据表 order by newid()"
随机取出数据库中的若干条记录的方法
top n,n就是要取出的记录数

Sql="select * from 数据表 where 字段名 in ('值1','值2','值3')"

╔----------------╗
☆ 添加数据记录 ☆
╚----------------╝
sql="insert into 数据表 (字段1,字段2,字段3 …) valuess (值1,值2,值3 …)"

sql="insert into 数据表 valuess (值1,值2,值3 …)"
不指定具体字段名表示将按照数据表中字段的顺序,依次添加

sql="insert into 目标数据表 select * from 源数据表"
把源数据表的记录添加到目标数据表

╔----------------╗
☆ 更新数据记录 ☆
╚----------------╝
Sql="update 数据表 set 字段名=字段值 where 条件表达式"

Sql="update 数据表 set 字段1=值1,字段2=值2 …… 字段n=值n where 条件表达式"

Sql="update 数据表 set 字段1=值1,字段2=值2 …… 字段n=值n "
没有条件则更新整个数据表中的指定字段值

╔----------------╗
☆ 删除数据记录 ☆
╚----------------╝
Sql="delete from 数据表 where 条件表达式"

Sql="delete from 数据表"
没有条件将删除数据表中所有记录)

引用以上函数的方法:
sql="select sum(字段名) as 别名 from 数据表 where 条件表达式"
set rs=conn.excute(sql)
用 rs("别名") 获取统的计值,其它函数运用同上。

╔----------------------╗
☆ 数据表的建立和删除 ☆
╚----------------------╝
CREATE TABLE 数据表名称(字段1 类型1(长度),字段2 类型2(长度) …… )
例:CREATE TABLE tab01(name varchar(50),datetime default now())
DROP TABLE 数据表名称 (永久性删除一个数据表)

╔--------------------╗
☆ 记录集对象的方法 ☆
╚--------------------╝
rs.movenext 将记录指针从当前的位置向下移一行
rs.moveprevious 将记录指针从当前的位置向上移一行
rs.movefirst 将记录指针移到数据表第一行
rs.movelast 将记录指针移到数据表最后一行
rs.absoluteposition=N 将记录指针移到数据表第N行
rs.absolutepage=N 将记录指针移到第N页的第一行
rs.pagesize=N 设置每页为N条记录
rs.pagecount 根据 pagesize 的设置返回总页数
rs.recordcount 返回记录总数
rs.bof 返回记录指针是否超出数据表首端,true表示是,false为否
rs.eof 返回记录指针是否超出数据表末端,true表示是,false为否
rs.delete 删除当前记录,但记录指针不会向下移动
rs.addnew 添加记录到数据表末端
rs.update 更新数据表记录

原文地址:https://www.cnblogs.com/wuyuankun/p/4102989.html