数据库SQL小试

来源:互联网 发布:淘宝3ds哪家的好 编辑:程序博客网 时间:2024/05/29 13:46

       在机房收费系统vb个人版的开发中我不断走进误区,又让师傅不断把我纠正过来。虽然在敲机房中出现了各种不尽人意的事情,但是我深刻体会到了自己存在的问题。对我来说,对自身问题的深刻认识才是项目实现中最金贵的东西。

      本次我来介绍一些数据库的东西,虽然东西不是很有水平,不过在机房收费的实现中却能帮我们节省大量的精力。宏观上来说,机房收费系统分前台软件和后台数据库支持。下面我来介绍一下具体的数据库操作:

      1、insert into values

           作用:向表中插入单个或多个记录

            TxtSQL = "insert into user_login values('" _
                           & "0" & "','" _
                           & "0" & "','" _
                           & "管理员" & "','" _
                           & "0" & "')"

           这里需要注意的是,对于常量需要用单引号来括起来。

     2、    TxtSQL = "select userid from user_login " _
                            & "except" _
                            & " select userid from teacheronline"

           作用:记录集的差运算

           举例:上面的SQL语句是用来得到总的教师人员但是不正在上机的教师。

    3、        TxtSQL = "select   rechargerecord.cardid,student_info.studentname,rechargerecord.addcash,rechargerecord.date,rechargerecord.time ,rechargerecord.head from rechargerecord inner join student_info" _ 
            & " on rechargerecord.cardid=student_info.cardid" _
            & " where date>='" & StartDate.Value & "' and date<='" & EndDate.Value & "'"

           作用:联合两个表,并按照自己的需要从两个表选取字段。

    4、    TxtSQL = "select coalesce(sum(cash),0) as MyFixed from student_info where type='固定用户'"
           作用:利用聚合函数,对字段求和。

           注意:这里coalesce的作用是,如果不存在记录那么返回0。如果不加这个函数,那么在没有记录的情况就会出现Null。

    5、    TxtSQL = "insert into backcashrecord select " & "cardid," & "studentid," & "studentname," _
            & "sex," & "department," & "grade," & "class," & "cash," & "type," _
            & "'" & Date & "','" & Time & "','" & UserCardID & "'" & "from student_info where cardid='" & txt_CardID.Text & "'"

          作用:从另外一个表中抽取全部或部分字段插入到本数据表中。

           上面的SQL语句的功能:把student_info表中cardidstudentidstudentname、sexdepartment

            gradeclasscashtype和当前日期、当前时间插入到backcashrecord表中。

       我能想到的大概也就这些了,能力有限,不足之处,望大家不吝赐教!!!

0 0
原创粉丝点击