实战SQL语句收集(不断更新中--)
2024-07-21 02:11:19
供稿:网友
 
实战sql语句收集(不断更新中--)
前言:这里将我编程实践中遇到的有价值的sql语句一路记下来,一方面方便自己查用,一方面也夯实下即将遗忘的回忆。整个过程中我会不断更新,直到不能再加为止,同时,这里只记录最实用的咚咚,不效仿学院派的那一套。
 
一、常用sql语句荟萃
1,查询:
1.1,简单查询:select * from table where 
 
1.2,连接查询:
什么是连接查询?顾名释义,就是查询时涉及多个表的查询。是以说到连接,废话一下,要知道连接还是关系数据库的主要特点呢。
连接查询分为三种:外连接(outer join),内连接(inner join),交叉连接(cross join)。
1.2.1,内连接(inner join)使用比较运算符进行表间某(些)列数据的比较操作,并列出这些表中与连接条件相匹配的数据行。根据所使用的比较方式不同,内连接又分为等值连接、自然连接和不等连接三种。
1.2.2,外连接分为左外连接(left outer join或left join)、右外连接(right outer join或right join)和全外连接(full outer join或full join)三种。与内连接不同的是,外连接不只列出与连接条件相匹配的行,而是列出左表(左外连接时)、右表(右外连接时)或两个表(全外连接时)中所有符合搜索条件的数据行。
1.2.3,交叉连接(cross join)没有where 子句,它返回连接表中所有数据行的笛卡尔积,其结果集合中的数据行数等于第一个表中符合查询条件的数据行数乘以第二个表中符合查询条件的数据行数。连接操作中的on (join_condition) 子句指出连接条件,它由被连接表中的列和比较运算符、逻辑运算符等构成。
1.2.4,无论哪种连接都不能对text、ntext和image数据类型列进行直接连接,但可以对这三种列进行间接连接。例如:
select p1.pub_id,p2.pub_id,p1.pr_info
from pub_info as p1 inner join pub_info as p2
on datalength(p1.pr_info)=datalength(p2.pr_info)
 
1.2.5,使用where子句设置查询条件
where子句设置查询条件,过滤掉不需要的数据行。例如下面语句查询年龄大于20的数据:
select *
from usertable
where age>20
where子句可包括各种条件运算符:
比较运算符(大小比较):>、>=、=、<、<=、<>、!>、!<
范围运算符(表达式值是否在指定的范围):between…and…
not between…and…
列表运算符(判断表达式是否为列表中的指定项):in (项1,项2……)
not in (项1,项2……)
模式匹配符(判断值是否与指定的字符通配格式相符):like、not like
空值判断符(判断表达式是否为空):is null、not is null
逻辑运算符(用于多条件的逻辑连接):not、and、or
1、范围运算符例:age between 10 and 30相当于age>=10 and age<=30
2、列表运算符例:country in ('germany','china')
3、模式匹配符例:常用于模糊查找,它判断列值是否与指定的字符串格式相匹配。可用于char、varchar、text、ntext、datetime和smalldatetime等类型查询。
可使用以下通配字符:
百分号%:可匹配任意类型和长度的字符,如果是中文,请使用两个百分号即%%。
下划线_:匹配单个任意字符,它常用来限制表达式的字符长度。
方括号[]:指定一个字符、字符串或范围,要求所匹配对象为它们中的任一个。
[^]:其取值也[] 相同,但它要求所匹配对象为指定字符以外的任一个字符。
例如:
限制以publishing结尾,使用like '%publishing'
限制以a开头:like '[a]%'
限制以a开头外:like '[^a]%'
空值判断符例:where age is null
2,更新:update table
 
3,插入:
3.1,一般插入:
insert into publishers
(pub_id, pub_name, city, state)
values
('9001', 'acme publishing', 'new york', 'ny') 
3.2,插入多行
使用 insert 语句可以向表添加多行数据。这些多行数据是从已经存有这些数据的另一个表中选取的。本例中,向 pubhold 表中添加有关在加利福尼亚和德克萨斯州的出版商的数据。这些数据可从 publishers 表中获得。
insert pubhpold select * from publishers
where state = 'ca' or state = 'tx' 
 
4,删除:
4.1,delete语句联合删除:
delete from uu_suitetominclassroomsect
where min_classroom_sect_id in 
   (select min_classroom_sect_id 
   from uu_minclassroomsect
   where min_classroom_id = '112')
二、视图使用细则
1,一个典型的视图
create view view_uugrouptaxis
as
select uu_groupinfo.group_id, uu_groupinfo.group_name, 
      uu_grouptype.main_type, uu_grouptype.group_type_name, 
      uu_groupinfo.group_icon_url, isnull
          ((select count(*)
          from uu_groupuser
          where uu_groupinfo.group_id = uu_groupuser.group_id), 0) 
      * 50 + isnull(uu_groupinfo.fundcount, 0) + isnull
          ((select count(*)
          from dv_topic
          where dv_topic.boardid = uu_groupinfo.subforum_id), 0) * 5 + isnull
          ((select count(*)
          from uu_groupphotos
          where uu_groupphotos.group_id = uu_groupinfo.group_id), 0) 
      * 10 + isnull(uu_groupinfo.topic_account, 0) 
      * 2 + isnull(uu_groupinfo.hit_account, 0) as group_activedegree, 
      isnull
          ((select count(*)
          from uu_groupuser
          where uu_groupinfo.group_id = uu_groupuser.group_id), 0) 
      as group_membernum, isnull(uu_groupinfo.fundcount, 0) as fundcount,
          (select count(*)
         from dv_topic
         where dv_topic.boardid = uu_groupinfo.subforum_id) as group_articlenum,
          (select count(*)
         from uu_groupphotos
         where uu_groupphotos.group_id = uu_groupinfo.group_id) as group_photonum, 
      uu_groupinfo.topic_account, uu_groupinfo.hit_account,
          (select user_name
         from uu_registeruser
         where uu_registeruser.user_id = uu_groupinfo.creator_id) 
      as group_creatorname, uu_groupinfo.create_time
from uu_groupinfo inner join
      uu_grouptype on 
      uu_groupinfo.group_type_id = uu_grouptype.group_type_id
 
三,存储过程的创建和调用
1,存储过程的调用
execute procedurename @param=’value’
2,一个典型的带参数存储过程
create procedure p_delminiclassproc 
 @miniclassroom_id int
as
declare @billtag varchar(4)
set nocount on
if @miniclassroom_id is null
         begin 
                   return(-1)
         end
else
                   begin transaction 
                   --删除套餐信息
                   delete from uu_suitetominclassroomsect
                   where min_classroom_sect_id in 
                      (select min_classroom_sect_id 
                      from uu_minclassroomsect
                      where min_classroom_id [email protected]_id)
                   --删除小课堂段信息
                   delete from uu_minclassroomsect
                   where min_classroom_id = @miniclassroom_id
                   --删除小课堂用户购买记录
                   delete from uu_userbuyminclassroom
                   where min_classroom_id = @miniclassroom_id
                   --删除对应小课堂年级学科信息
                   delete from uu_minclassroomtogradeclass
                   where min_classroom_id = @miniclassroom_id
                   --删除小课堂发言
                   delete from uu_minclassroomdiscuss
                   where min_classroom_id = @miniclassroom_id
                   --删除课程讨论
                   delete from uu_coursediscuss
                   where course_id in 
                      (select course_id 
                      from uu_courseinfo
                      where min_classroom_id = @miniclassroom_id)
                   --删除用户课程收藏
                   delete from uu_usercellectioncourse
                   where course_id in 
                      (select course_id 
                      from uu_courseinfo
                      where min_classroom_id = @miniclassroom_id)
                   --删除小课堂小课程的用户购买信息
                   delete from uu_userbuycourse
                   where course_id in 
                      (select course_id 
                      from uu_courseinfo
                      where min_classroom_id = @miniclassroom_id)
                   commit  transaction
                   return(1)
go
四,触发器
网站运营seo文章大全提供全面的站长运营经验及seo技术!