ASP + Access 初阶笔记 By Shawl.qiu

ASP + Access 初阶笔记 By Shawl.qiu


说明: 
这是 shawl.qiu 的个人笔记, 一点一滴都是敲出来, 一大堆, 杂乱无序, 不过初级 Access 的操作内容应该都涉及到了. 
所以, 转载什么的请注明一下来源, 多次见着鄙人写的一些不成文的文章被转到这呀那呀, 某些人对作者很不尊重, 连注明出处都没. 

这些内容只适合初阶水平的人阅读, 大侠类人物请无视. 

shawl.qiu
  http://blog.csdn.net/btbtd 

  1. linenum
  2. CREATED BY STABX, AT 2006-4-12.
  3.  
  4. ASP and Access Database Notes
  5.  
  6. ---/---------------------------------------------------------
  7.  
  8. general operate
  9.  
  10. 11. 获得新插入数据的 ID
  11.  
  12. 11.1 获取单个 ID
  13. ------------------------------------------------------------------ 
  14. sql = "INSERT INTO ctarticle (classid,nclassid,title,content,dateandtime,writer,email,www,url,reoff,htmloff,ubboff ) VALUES ("&cid_&","&ncid_&",'"&trim(tt_)&"','"&ct_&"','"&adt_&"','"&trim(au_)&"','"&trim(em_)&"','"&trim(www_)&"','"&trim(url_)&"',"&ro_&","&ho_&","&uo_&") " '插入数据操作
  15. rsData.Execute(sql)
  16. set rs1=rsData.Execute("select max(articleid) from ctarticle")
  17. response.write rs1(0)
  18. rs1.close : set rs1=nothing    
  19. ------------------------------------------------------------------ 
  20.  
  21. 11.2 获取多个 ID
  22. ------------------------------------------------------------------ 
  23. sql = "INSERT INTO ctarticle (classid,nclassid,title,content,dateandtime,writer,email,www,url,reoff,htmloff,ubboff ) VALUES ("&cid_&","&ncid_&",'"&trim(tt_)&"','"&ct_&"','"&adt_&"','"&trim(au_)&"','"&trim(em_)&"','"&trim(www_)&"','"&trim(url_)&"',"&ro_&","&ho_&","&uo_&") " '插入数据操作
  24. rsData.Execute(sql)
  25. set rs1=rsData.Execute("select top 1 * from ctarticle order by articleid desc")
  26. response.write rs1("articleid")
  27. response.write " "
  28. response.write rs1("classid")
  29. response.write " "
  30. response.write rs1("nclassid")
  31. rs1.close : set rs1=nothing
  32. ------------------------------------------------------------------ 
  33.  
  34. 10. 日期
  35.  
  36. 9. 列出不是 汉字 和 字母 和 号码 的数据
  37.  
  38. 8. 为不同数据库表中的相同列赋值, 并进行条件查询
  39.  
  40. 7. 统计记录数
  41.  
  42. 6. 以两个列进行排列
  43.  
  44. 5. 查询一个范围内的数据
  45.  
  46. 4. 查询多个数据表
  47.  
  48. 3. 计数器加 1
  49.  
  50. 2. 用于在某页面显示某分类的查询法, 并用 ORDER BY 排序
  51.  
  52. 1. DW 链接 ACCESS 数据库
  53.  
  54. //----------------------
  55.  
  56. 10.1 查找下一年中的第一条记录
  57. sql="SELECT top 1 dateandtime FROM ctarticle where year(dateandtime)=(year(#"&date&"#))"&norp
  58.  
  59. 10. 日期
  60.  
  61. 9. 列出不是 汉字, 字母, 数字 开头的数据
  62. sql="select * from ctarticle where (title not between '阿' and '做祚') and (title not like '[0-9a-z]%')"
  63. //Access 中 用 * 代替 % 号 
  64.  
  65. 8. 为不同数据库表中的相同列赋值, 并进行条件查询
  66. "SELECT article.*, class.* FROM article, class WHERE article.classid=class.classid and article.classid="&request("classid")
  67. // SELECT 表1所有列,表2所有列 FROM 表1,表2 WHERE 表1.栏1=表2.栏1 AND 表1.栏1=条件
  68.  
  69. 7. 统计记录数
  70. <% 
  71.     set    rsC=server.CreateObject("adodb.recordset")
  72.         rsC.ActiveConnection=MM_conn_STRING
  73.         rsC.open "select count(*) as totalC from article",MM_conn_STRING
  74. %>
  75. <%= rsC("totalC") %>
  76. <% 
  77.     rsC.Close()
  78.     set rsC=nothing 
  79. %>
  80.  
  81. 6. 以两个列进行排列
  82. "SELECT * FROM nclass order by classid,nclassid "
  83. // SELECT 所有 FROM 数据库表 ORDER BY 先按 classid 进行排序, 再按 nclassid 进行排序
  84.  
  85. 5. 查询一个范围内的数据
  86. Recordset1.Source = "SELECT articleid, content, nclassid FROM article where (articleid<=2064 and articleid>=2037) "
  87. //此语句查询 articleid 2037 至 2064 之间的所有数据
  88.  
  89. 4. 查询多个数据表
  90. "SELECT class.*,nclass.*,article.* FROM class,nclass,article WHERE article.classid=class.classid and article.nclassid=nclass.nclassid and article.articleid="&Request.QueryString("articleid")
  91. //先用 "SELECT 表.*" 查询表中所有列, 再用 "FROM 数据表" 选择查询的数据表, 再用 "WHERE 表.列=表.列" 合并各个数据表相同中的名称
  92.  
  93. 3. 计数器加 1
  94. "UPDATE article SET hits=hits+1 WHERE articleid="&request("articleid")
  95.  
  96. 2. 用于在某页面显示某分类的查询法, 并用 ORDER BY 排序
  97. "SELECT articleid, classid, title FROM article where classid="& Request.QueryString("classid") & " ORDER BY articleid DESC"
  98. // 注意 ORDER BY 的空格!!!  这个地方:  " ORDER BY articleid DESC"
  99.  
  100. 1. DW 链接 ACCESS 数据库
  101. conn
  102. "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("xxx.mdb")
  103.  
  104. ---/---------------------------------------------------------
  105.  
  106. important skill
  107.  
  108. 2. 数据查询
  109.  
  110. 1. 翻页 / 问题解决 2006-4-23
  111.  
  112. ---/---------------------------------------------------------
  113.  
  114. GENERAL KNOWLEDGE
  115.  
  116. 4. 外部连接和自联接
  117. inner join(等值连接) 只返回两个表中联结字段相等的行
  118. left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录
  119. right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录
  120. on 指定表间联结字段及其关系的等号 "=" 表达式, 返回 true 或 false. 当表达式返回 true 时, 则查询中包含该记录. 
  121. ! 外部连接只能操作已存在于数据库中的数据
  122.  
  123. 3. insert into 表 (列1,列2) values(数字,'字符')
  124. 例: "INSERT INTO article  (classid,nclassid,title,content )  VALUES (1,1,'whattttttt','cr') "
  125. // 插入数据到所选列
  126.  
  127. 2. drop table 表
  128. // 删除表
  129.  
  130. 1.1 查找不是 A-Z 和 0-9 开头的内容
  131. select * from ctglossary where gname not like '[A-Z0-9]%[a-z0-9]' order by gname desc
  132.  
  133. 1. SELECT 列 FROM 表 WHERE 行 ORDER BY 序
  134.  
  135. ---/---------------------------------------------------------
  136.  
  137. 数据库操作语句
  138.  
  139. 16. having 子句 / 筛选结果
  140.  
  141. 15. 文本函数
  142.  
  143. 14. Group By / 总结数据 
  144.  
  145. 13. 时间日期
  146.  
  147. 12. nz 行函数 / 替换空值
  148.  
  149. 11.1 视图操作例 一
  150. 11. ASP 操作 Access 视图 
  151.  
  152. 10. iif 函数
  153.  
  154. 9. 自连接
  155.  
  156. 8. union | union all / 联合查询
  157.  
  158. 7. 外连接——交叉查询
  159. 7.1 查询 
  160. 7.2 等值连接
  161. 7.3 右外连接
  162. 7.4 左外连接
  163. 7.5 更新操作
  164.  
  165. 6. 内连接 | Select 别名1.列, 别名2.列 FROM 表1 别名1, 表2 别名2
  166.  
  167. 5. 列操作 | connection&alter table 表名 add/drop column 列名
  168.  
  169. 4. 删除数据 | connection&delte from
  170.  
  171. 3. 更新数据 | connection&update
  172.  
  173. 2. 插入数据 | connection&Insert into 
  174.  
  175. 1. 连接数据库 | Recordset&Select
  176.  
  177. 0. 查找数据库中的表/视图
  178.  
  179. -1. 创建 B表, 并从 A表中 选择数据插入 B表
  180.  
  181. //----------------------
  182.  
  183. 16.1 查找标题重复 大于或等于 2 的文章
  184. select title, count(articleid) as ct from ctarticle group by title having count(articleid)>1
  185. //解读
  186. //选择 标题, 计算(文章ID) 赋 别名  从 表 总结 标题 有 计算(文章ID) 大于 2 
  187. 注: 计算别名在 having 子句里没有意义
  188.  
  189. 16. having 子句 / 筛选结果
  190.  
  191. 15.3 trim(gname) //格式函数
  192. UPDATE tgg SET gname = trim(gname);
  193. //解读
  194. //更新 表 设置 列=删除两边空格(列)
  195.  
  196. 15.2.1 从上例中加入逻辑判断
  197. update tgg set subgname=replace(subgname,'<' or '{','') 
  198. //实例操作中此语句运行无效, 还是下面方法管用
  199. update tgg set subgname=replace(subgname,'{','') 
  200.  
  201. 15.2 replace(字段名,查找字符,替换字符) //替换函数 
  202. update tgg set subgname=replace(subgname,'<','') 
  203. //解读
  204. //更新 表 设置 列=替换(列,字符a,字符b)
  205.  
  206. 15.1.2 有 mid 就有 left, right
  207. 15.1.2.2
  208. UPDATE tgg SET subgname = left(gname,15);
  209. 15.1.2.1 
  210. UPDATE tgg SET subgname = right(gname,15);
  211.  
  212. 15.1 mid(name,start by num,end by num) //提取字段中的字符
  213. UPDATE ctglossary SET subgname = mid(gname,1,20)
  214. WHERE subgname='unsubtitle';
  215. //更新 表 设 列1 = MID(列2,1,20) //提取 列1 的前20 个字符更新给 列1
  216. 在哪里 列1 等于 条件
  217.  
  218. 15. 文本函数
  219.  
  220. 14.1 显示父类数量, 子类数量, 文章数量. 结果为级联拥有的数量
  221. select classid,nclassid,count(*) as articles from ctarticle group by classid,nclassid
  222. //解读
  223. //选择 ID1, ID11, 计算(所有) 赋 别名 从 表 总结 ID1, ID11
  224. 结果如:
  225. classid | nclassid | articles
  226. 1 + 10 + 100
  227. etc...
  228. //类似于数组显示            
  229.  
  230. 14. Group By / 总结数据 
  231. 心得: Group By 功能为总结相同的数据, 并适当分组显示. 如果结合 having 子句, 可以实现 where 的筛选功能, 也就是减少操作步骤, 只是一步. 
  232.  
  233. 13.5.1 列出某一天, 上一星期的数据
  234. SELECT distinct dateandtime
  235. FROM ctdate_by_query_date
  236. WHERE dateandtime between ((#2006-5-15#+(Weekday(2006-5-15)-7))-6) and (#2006-5-15#-7)+Weekday(2006-5-15)
  237. sql="SELECT distinct dateandtime FROM ctarticle WHERE dateandtime between ((#"&date&"#+(Weekday("&date&")"&norp&"7))-6) and (#"&date&"#"&norp&"7)+Weekday("&date&")"
  238.  
  239. 13.5 查询一天, 所隶属星期所有天数的数据
  240. SELECT *
  241. FROM ctdate_by_query_date
  242. WHERE dateandtime between ((#2006-5-15#+Weekday(2006-5-15))-6) and #2006-5-15#+Weekday(2006-5-15)
  243.  
  244. 13.4 查询一个时间段
  245. SELECT *
  246. FROM ctdate_by_query_date
  247. WHERE dateandtime between #2006-5-1# and #2006-5-30#
  248.  
  249. 13.3.2 列出不同年份的年份, 并且不要相同
  250. select distinct year(dateandtime) from ctarticle
  251. 结果如: 
  252. Expr1000
  253. 2000
  254. 2003
  255. 2004
  256. 2005
  257. 2006
  258.  
  259. 13.3.1 列出某一天, 上一年的第一条记录
  260. SELECT top 1 dateandtime FROM ctarticle where year(dateandtime)=(2006)-1
  261. sql="SELECT top 1 dateandtime FROM ctarticle where year(dateandtime)=(year(#"&date&"#))"&norp
  262.  
  263. 13.3 列出某一年的数据
  264. sql="select * from ctdate_by_query_date where year(dateandtime)="&year(rqqdt_)&" order by dateandtime desc"
  265.  
  266. 13.2.1 查找上一月中的第一条记录
  267. SELECT top 1 dateandtime FROM ctarticle where year(dateandtime)=year(#2006-5-28#) and  month(dateandtime)=month(#2006-5-28#)-1
  268.  
  269. 13.2 列出某一月的数据
  270. sql="select * from ctdate_by_query_date where year(dateandtime)="&year(rqqdt_)&" and month(dateandtime)="&month(rqqdt_)&" order by dateandtime desc"
  271.  
  272. 13.1 列出某一日的数据
  273. sql="select * from ctdate_by_query_date where dateandtime=#"&rqqdt_&"# order by a.articleid desc"
  274.  
  275. 13. 时间日期
  276. 例一: 列出当天的数据
  277. sql="select * from ctdate_by_query_date where dateandtime=date() order by a.articleid desc"
  278.  
  279. 12.3 更新多个值的空值为 ##
  280. UPDATE ctglossary SET 
  281. gname = nz(gname,"untitle"), 
  282. gcontent = nz(gcontent,"gcontent empty..."), 
  283. submiter = nz(submiter,"anonymous"), 
  284. gsource = nz(gsource,"unknown"), 
  285. gurl = nz(gurl,"/"), 
  286. submitermail = nz(submitermail,"@"),
  287. subgname = nz(subgname,"unsubtitle")
  288.  
  289. 12.2 更新空值为 ##
  290. update tspic set pname=nz(pname,'no name')
  291. //解读
  292. //更新 表 设置 列=替换空值(列,'没有名字')
  293.  
  294. 12.1 替换空值为 ## 
  295. select picid, nz(pname,'no name') from tspic
  296. //解读
  297. //选择 列1, 替换空值(列2,'没有名字') 从 表  
  298.  
  299. 12. nz 行函数 / 替换空值
  300.  
  301. 11.1.2 ASP 连接视图操作
  302. rs.open "select * from date_by_query_date where dateandtime=#"&rqqdt_&"#",MM_conn_String 
  303. //何时使用视图, 在单表操作时, 不需要考虑视图; 在多表操作时, 应该使用视图, 表一多, 逻辑就复杂, 复杂就容易出错. 
  304.  
  305. 11.1 视图操作例 一
  306. 11.1.1 建立视图, 并保存为: date_by_query_date
  307. SELECT a.*, b.*, c.*, d.*
  308. FROM ((ctarticle AS a LEFT JOIN ctclass AS b ON a.classid = b.classid) LEFT JOIN ctnclass AS c ON a.nclassid = c.nclassid) LEFT JOIN cttag AS d ON a.articleid = d.articleid;
  309.  
  310. 11. ASP 操作 Access 视图 
  311. 注: 操作视图跟一般操作原理一样, 只不过操作视图的时候, 数据先经过筛选. 
  312.  
  313. 10.1 判断 articleid 是事大于 2000, 满足条件打印出 '不错'
  314. select iif(articleid>2000,'不错',' ') as ok from ctarticle where articleid between 1990 and 2050
  315.  
  316. 10. iif 函数
  317.  
  318. 9.1 自连接1
  319. select top 10 a.title, a.articleid, b.title as title2, b.articleid as articleid2 from ctarticle a,ctarticle b
  320.  
  321. 9. 自连接
  322.  
  323. 8.3 union 编写全外连接
  324. select a_cs.*, b_ncs.* 
  325. from ctclass a_cs left join ctnclass b_ncs on a_cs.classid=b_ncs.classid
  326. union
  327. select c_cs.*, d_ncs.*
  328. from ctclass c_cs right join ctnclass d_ncs on c_cs.classid=d_ncs.classid
  329. //解读
  330. 选择 别名1.所有, 别名2.所有
  331. 从 表1 别名1 左外连接 表2 别名2 在 别名1.列1=别名2.列1
  332. 联合到
  333. 选择 别名3.所有, 别名4.所有
  334. 从 表1 别名3 右外连接 表2 别名4 在 别名3.列1=别名4.列1
  335.  
  336. 8.2 select top 10 articleid,classid from ctarticle UNION all select top 10 articleid,classid from cttag;
  337. //union all 不删除重复行, 不进行自动排序, 执行效率高. 
  338. 注: 做事效率明显会降低
  339.  
  340. 8.1 select top 10 articleid,classid from ctarticle UNION select top 10 articleid,classid from cttag;
  341. 一般联合查询
  342. //union 查询自动删除重复行和排序
  343.  
  344. 8. union | union all / 联合查询
  345.  
  346. 7.5.2
  347. UPDATE (ctarticle AS a LEFT JOIN ctclass AS c ON a.classid = c.classid) LEFT JOIN cttag AS b ON a.articleid = b.articleid 
  348. SET tag=tag+' ', b.articleid=a.articleid, b.classid=a.classid, b.nclassid=a.nclassid 
  349. WHERE a.classid=23 AND a.nclassid=0 AND tagid is not null
  350.  
  351. 7.5.1  
  352. UPDATE (ctarticle AS a LEFT JOIN (ctnclass AS c LEFT JOIN ctclass AS d ON c.classid = d.classid) ON a.nclassid = c.nclassid AND a.classid = c.classid) LEFT JOIN cttag AS b ON a.articleid = b.articleid SET tag=d.class+' '+c.nclass, b.articleid=a.articleid, b.classid=a.classid, b.nclassid=a.nclassid
  353. WHERE a.classid=23 AND a.nclassid=197;
  354.  
  355. 7.5 更新操作
  356.  
  357. 74.5 左连接中数据的筛选
  358. INSERT INTO cttag(articleid,classid,nclassid) SELECT a.articleid,a.classid,a.nclassid from ctarticle a left join cttag b on a.articleid=b.articleid where b.articleid is null
  359. //本语句功能为, 显示主表的全部内容, 插入数据到副表中没有的数据
  360. //主要作用为: 让数据减少冗余
  361.  
  362. 7.4.4.1 上例中的延续
  363. SELECT a.*, b.*, c.*, d.*
  364. FROM cttag as d left join ((ctarticle AS a LEFT JOIN ctclass AS b ON a.classid=b.classid) LEFT JOIN ctnclass AS c ON a.nclassid=c.nclassid) on d.articleid=a.articleid;
  365.  
  366. 7.4.4 显示文章表中的全部, 调用类别表中的栏目
  367. select a.*, b.*, c.* from (ctarticle a left join ctclass b on a.classid=b.classid) left join ctnclass c on a.nclassid=c.nclassid
  368. //作用, 有时在文章表中包含了在个别类别表中没有的数据, 用这个语法可以读出文章表的全部数据
  369. //a 为 文章表, b 为主类别, c 为子类别
  370.  
  371. 7.4.3 同上例, 选择追加数据时加上空格
  372. INSERT INTO cttag(articleid,classid,nclassid,tag)
  373. SELECT a.articleid,a.classid,a.nclassid,d.class+' '+c.nclass
  374. FROM (ctarticle AS a left join (ctnclass c left join ctclass d on c.classid=d.classid) on a.classid=c.classid and a.nclassid=c.nclassid) LEFT JOIN cttag AS b ON a.articleid = b.articleid where a.classid=4 and a.nclassid=154;
  375.  
  376. 7.4.2 连接N个表, 并追加数据到其中一个表, N=4
  377. INSERT INTO cttag(articleid,classid,nclassid,tag)
  378. SELECT a.articleid,a.classid,a.nclassid,d.class+c.nclass
  379. FROM (ctarticle AS a left join (ctnclass c left join ctclass d on c.classid=d.classid) on a.classid=c.classid and a.nclassid=c.nclassid) LEFT JOIN cttag AS b ON a.articleid = b.articleid where a.classid=1 and a.nclassid=1;
  380. //解读
  381. 插入到 表2(栏1,栏2,栏3,栏4)
  382. 选择 别名a.栏1, 别名a.栏2, 别名a.栏3, 别名d.栏4  加上 别名c.栏5
  383. 从 (表1 别名a 左连接 (表3 别名c 左连接 表4 别名d 在 别名c.栏2 等于 别名d.栏2) 在 别名a.栏2 等于 别名c.栏2 和 别名a.栏3=别名c.栏3) 左连接 表2 别名b 在 别名a.栏1 等于 别名b.栏1 在那里 别名a.栏2=1 和 别名a.栏3=1
  384.  
  385. 7.4.1 连接两个表, 并追加数据到其中一个表
  386. INSERT INTO cttag(articleid,classid,nclassid)
  387. SELECT a.articleid,a.classid,a.nclassid
  388. FROM ctarticle AS a LEFT JOIN cttag AS b ON a.articleid = b.articleid where a.classid=1 and a.nclassid=1;
  389. //解读
  390. 插入到 表2(栏1,栏2,栏3)
  391. 选择 别名a.栏1, 别名a.栏2, 别名a.栏3
  392. 从 表1 别名a 左连接 表2 别名b 在  别名a.栏1 等于 别名b.栏1 在那里 别名a.栏4=1 和 别名a.栏5=1
  393.  
  394. 7.4. 左连接
  395.  
  396. 7.3.1 同步两表的数据
  397. UPDATE ctarticle a INNER JOIN cttag b ON a.articleid = b.articleid SET b.classid=a.classid, b.nclassid=a.nclassid;
  398. //解读
  399. 更新 表1 别名a 联接 表2 别名2 在 别名a.栏1 等于 别名b.栏1 设置  别名b.栏2 更新为 别名a.栏2,  别名b.栏3 更新为 别名a.栏3  
  400.  
  401. 7.3 右外连接
  402. select a.*, b.* from bunclass a right join ctclass b on a.classid=b.classid where a.nclassid=20
  403. 查询别名 a,b 表, 只匹配 b 表中的内容.
  404.  
  405. 7.2.3 添加数据到连接表之一
  406. INSERT INTO cttag ( tag, articleid ) SELECT top 1 b.tag, a.articleid FROM ctarticle AS a left JOIN cttag AS b ON a.articleid = b.articleid WHERE a.articleid order by a.articleid desc;
  407.  
  408. 7.2.2 变通中的用法二
  409. INSERT INTO bureply 
  410. SELECT b.*, a.classid, a.nclassid
  411. FROM article AS a INNER JOIN reply AS b ON a.articleid = b.articleid
  412. WHERE classid=50;
  413.  
  414. 7.2.1 实际应用中的变通
  415. INSERT INTO butag ( tag, articleid, classid, nclassid)
  416. SELECT b.tag, a.articleid, a.classid, a.nclassid
  417. FROM article AS a INNER JOIN tag AS b ON a.articleid = b.articleid
  418. WHERE classid=24;
  419.  
  420. 7.2 添加数据到其他表
  421. INSERT INTO butag ( tag, articleid )
  422. SELECT b.tag, a.articleid
  423. FROM article AS a INNER JOIN tag AS b ON a.articleid = b.articleid
  424. WHERE a.articleid<>False;
  425. //解读
  426. 添加到 接收表(列1,列2)
  427. 选择 别名b.列1, 别名a.列2
  428. 从 表1 表名a 联接 表2 表名b 在 别名a.列c 等于 别名b.列c
  429. 在哪里 别名a.列c 不等于 没有
  430.  
  431. 7.1.1 实际应用中的变通
  432. SELECT b.tag, a.articleid, a.classid, a.nclassid
  433. FROM article AS a INNER JOIN tag AS b ON a.articleid = b.articleid
  434. WHERE a.classid=24;
  435.  
  436. 7.1 查询
  437. SELECT b.tag, a.articleid
  438. FROM article AS a INNER JOIN tag AS b ON a.articleid = b.articleid
  439. WHERE a.articleid<>False;
  440. //解读
  441. 选择 别名b.列, 别名a.列
  442. 从 表1 别名a 联接 表2 别名b 在 别名a.列c = 别名b.列c
  443. 在哪里 别名a.列c 不等于 没有
  444. 注: as 不是必要
  445.  
  446. 7. 外连接——交叉查询
  447. 提示: 注意表中相同的栏目
  448.  
  449. 6. 内连接
  450. SELECT a.*, b.* FROM article a, nclass b where b.nclassid=44;
  451. 选择 别名1.所有, 别名2.所有 来自 表1 赋别名, 表2 赋别名 在什么地方 别名2=44
  452.  
  453. 5.1 从表中删除列——Access 数据视图操作
  454. alter table news
  455. drop column 
  456. url,
  457. www,
  458. writer,
  459. email,
  460. classid,
  461. nclassid,
  462. hits,
  463. dateandtime;
  464. //从 News 表 删除 栏目 栏目名
  465.  
  466. 5.从表中添加列——Access 数据库视图操作
  467. alter table news
  468. add column 
  469. url varchar(255),
  470. www varchar,
  471. writer varchar,
  472. email varchar,
  473. classid byte,
  474. nclassid byte,
  475. hits byte,
  476. dateandtime datetime;
  477. //从 News 表 添加 栏目 栏目名 数据类型
  478.  
  479. 4.1 delete from article where classid=23 and nclassid=156;
  480.  
  481. 4. 删除数据
  482. <% '删除数据
  483.     set rsDelete=server.CreateObject("adodb.connection")
  484.         rsDelete.open MM_conn_String
  485.         
  486.         '注意, 输入为数字不能加单引号
  487.         sql= "DELETE FROM article WHERE articleid in(3263, 3262, 3261)" '删除 ID 为 3263, 3262, 3261 的文章
  488.         
  489.         rsDelete.execute(sql)
  490.         
  491.         rsDelete.close
  492.     set rsDelete=nothing
  493.  
  494.     response.Redirect("../index.asp") '返回首页
  495. %>
  496.  
  497. 3.3 同步两表中的数据
  498. UPDATE ctarticle AS a, ctarticle1 AS b SET a.title=b.title
  499. WHERE a.articleid=b.articleid;
  500.  
  501. 3.2 在本列的值上加上另一列, 也叫合并列, 但是只合并数据到列, 而不合并列本身.
  502. UPDATE GiArticles SET content=c1+content;
  503.  
  504. 3.1 更新数据库表中的多个行
  505. <%    '更新数据库表中的多个行
  506.      set    rsUpdate=server.CreateObject("adodb.connection")
  507.         rsUpdate.open MM_conn_String
  508.         
  509.         sql = "update test set id=0 where id is null"
  510.         
  511.         rsUpdate.execute(sql)
  512.         rsUpdate.close
  513.     set    rsUpdate=nothing
  514. %>
  515.  
  516. 3. 更新数据
  517. <% '更新数据
  518.     set rsUpdate=server.CreateObject("adodb.connection")
  519.         rsUpdate.open MM_conn_string
  520.  
  521.         '数字不加单引号, 字符加, 日期加井号
  522.         sql= "update article set classid="&classid_&", nclassid="&nclassid_&", writer='"&writer_&"', email='"&email_&"', www='"&www_&"', url='"&url_&"', title='"&title_&"', content='"&content_&"' where articleid="&request("articleid")
  523.         
  524.         rsUpdate.execute(sql)
  525.  
  526.         rsUpdate.close
  527.     set rsUpdate=nothing
  528. %>
  529.  
  530. 2.1.2
  531. INSERT INTO article(content, title, url, www, writer, email, classid, nclassid, hits, dateandtime) select content, title, url, www, writer, email, classid, nclassid, hits, dateandtime from newbb_backup;
  532.  
  533. 2.1.1 从不同的数据库表选择数据并插入 
  534. sql = "INSERT INTO articleInsertTest(title,content) select top 50 title,content from Newbb_posttext"
  535.  
  536. 2.1 
  537. <%    '从不同的数据库表选择数据并插入 
  538.     set rsInsert = Server.CreateObject("ADODB.connection")
  539.         rsInsert.open MM_conn_STRING '数据库连接字符串
  540.  
  541.         '注意单引号的使用
  542.         sql = "INSERT INTO buarticle select article.* from article where articleid=3445 " 
  543.         '从 article 表选择 articleid=3445 的数据, 插入到 buarticle 表
  544.  
  545.         rsInsert.Execute(sql) '执行数据库插入语句
  546.  
  547.         rsInsert.close
  548.     set rsInsert=nothing
  549. %>
  550.  
  551. 2. 插入数据
  552. <%    '插入数据
  553.     set rsInsert = Server.CreateObject("ADODB.connection")
  554.         rsInsert.open MM_conn_STRING '数据库连接字符串
  555.  
  556.         '注意单引号的使用
  557.         sql = "INSERT INTO article (classid,nclassid,title,content )  VALUES ("&classid_&","&nclassid_&",'"&title_&"','"&content_&"' ) " '选择列, 并插入数据
  558.  
  559.         rsInsert.Execute(sql) '执行数据库插入语句
  560.  
  561.         rsInsert.close
  562.     set rsInsert=nothing
  563. %>
  564.  
  565. 1.2 参数查询
  566. SELECT *
  567. FROM ctarticle AS a
  568. WHERE a.classid=[@classid o, please input] AND a.nclassid=[@nclassid];
  569.  
  570.  
  571. 1.1 子查询
  572. "select top 10 * from (select top 1000 * from ctarticle) a"
  573.  
  574. 1. 连接数据库
  575. <%    '连接数据库
  576.     set rs=server.CreateObject("adodb.recordset") '创建 rs 数据查询
  577.         rs.open "select * from article",MM_conn_String '数据查询语句
  578.             
  579.         response.Write rs("title") '简单读出数据
  580.         
  581.         rs.close '关闭 rs 连接
  582.     set rs=nothing 
  583. %>
  584.  
  585. 0. 查找数据库中的表
  586. <% '查找数据库中所有数据库表名, 与视图为查询类型的视图
  587.     set rs=server.createobject("Adodb.connection")
  588.         rs.open MM_conn_string
  589.             set rsSchema=rs.openSchema(20)'20 指定查找表,视图; 23 指定查找视图
  590.                 while not rsSchema.EOF
  591.                     if rsSchema("TABLE_TYPE")="TABLE" then '显示表名
  592.                          response.write "/--- "
  593.                          response.write rsSchema("TABLE_NAME") 
  594.                          response.write  "<br>"    
  595.                     end if
  596.                     if rsSchema("TABLE_TYPE")="VIEW" then '显示视图名
  597.                          response.write "-----------------------/ "
  598.                          response.write rsSchema("TABLE_NAME") 
  599.                          response.write  "<br>"    
  600.                     end if
  601.                         rsSchema.movenext
  602.                 wend 
  603.                     rsSchema.close
  604.             set rsSchema=nothing
  605.         rs.close
  606.     set rs=nothing 'code by shawl.qiu
  607. %>
  608.  
  609. -1.1 从 A 表创建 B 表, 只选择 A 表的结构
  610. select * into tshm from ctarticle where 1=0
  611.  
  612. -1. 创建 B表, 并从 A表中 选择数据插入 B表
  613. select articleid,title into ctarticle1 from ctarticle
  614.  
  615. ---/---------------------------------------------------------
  616. Problems List
  617. -----------------------
  618. Current Item 5
  619.  
  620. -----------------------
  621.  
  622. 1. 列出所有年份, 并且不重复显示, 只显示该年一次.  20:30 2006-5-28
  623. 例: 2001, 2002, 2003, 2004
  624.  
  625. 2. 列出所有月份, 并且不重复显示, 只显示年中不同月份一次.  20:30 2006-5-28
  626. 例: 2001-08, 2001-09, 2001-10, 2001-11
  627.  
  628. 3. 同时更新多个表的数据
  629. //不可能同时更新, 只能分线程更新 20:30 2006-5-28
  630.  
  631. 4. ASP 可否使用视图, 或者如何使用视图 20:28 2006-5-28
  632.  
  633. 5. ASP 按拼音查找数据 3:03 2006-5-17 20:28 2006-5-28
  634.  
  635. ---/---------------------------------------------------------
  636.  
  637. Access 操作相关
  638.  
  639. 5. 在插入数据时, 如果在一个 RS 连接未关闭的时候, 在其中再选择最末的数据, 那只会选择在未插入数据时最本的一条.
  640.  
  641. 4. 查找替换框中 正则的应用
  642. 4.1 ?[}]*
  643. 4.2 ?[>]*
  644.  
  645. 3. 在知道要取出多少条语录时用 for 循环, 不知道用 while 循环。
  646. while 循环 有一个小毛病, selete top * 在指定 排序栏目的 ID 出现重复的时候,会出错。
  647.  
  648. 2. 页面调用数据库表的内容, 应分为显示主表和副表. 15:18 2006-5-19
  649. 用外连接显示主表的全部内容, 副表只作为配称. 
  650.  
  651. 1. 在 Access 操作视图中, 可能的话, 不要使用关系. 20:13 2006-5-18
  652. !但在数据库语句中喜欢怎么用就怎么用.
  653.  
  654. ---/---------------------------------------------------------
  655.  
  656. 建表必须
  657.  
  658. 2. 前缀
  659. 命名表应考虑适当的前缀
  660.  
  661. 1. 日期时间
  662. !日期 与 时间 应该为不同的两个列, 方便数据调用 
  663.  
  664. ---/---------------------------------------------------------
  665.  
  666. 建表需要考虑的列
  667.  
  668. 3. 权限列
  669.  
  670. 2. 排序 ID
  671. 方便管理
  672. ! 排序 ID 应设置两个, 一个为版面, 一个为导航.
  673.  
  674. 1. tag 
  675. 标签, 现在流行, 也方便
  676.  
  677. ---/---------------------------------------------------------
  678.  
  679. 数据库不能做的事
  680.  
  681. 1. 不能同时添加全新的多个行, 只能分开添加; 添加全新的多个行只能取材于数据库已有的行.
  682.  
  683. ---/---------------------------------------------------------
  684.  
  685. 规则
  686.  
  687. 1. 在默认情况下, 当查找表主键中的值出现在数据表的外键中时, 不允许更改或者删除这些数值. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值