您的位置:首页 > 博客中心 > 数据库 >

最近使用到的一些SQL语句

时间:2022-03-14 02:54

1.查询重复的信息:

select *
from Rex_Test
where tName in (select tName from Rex_Test group by tName having count(1) >= 2)

2.从表中随机读取N条数据

Select top N * From table Order by newid() 


3.多条件查询时,当条件有为空的情况

select * from usertable where 1=1 and (name=@name or @name='') and (page=@page or @page='')


4.在原有字段内容的基础上再添加新的部分,如a,添加上bc,就变成了abc:

update table1 set field1= field1+'ef' where field1='abcd'

5.在原有字段内容上删除某一部分:

update  tOrderCustomerList  set  ViewAuthority=replace(ViewAuthority,'0041101009|01012','')
//就是把字段ViewAuthority中的“<span style="font-family: Arial, Helvetica, sans-serif;">0041101009|01012</span><span style="font-family: Arial, Helvetica, sans-serif;">”替换为空</span>

6.查询某字符内容为指定格式的数据:

查询格式为“XX######-####”这样的数据,
其中X为字母,#为数字


前面两位字母如果不是固定为aa
select * from 表 where A LIKE '[a-zA-Z] [a-zA-Z] [0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]'



如果前两位固定aa
select * from 表 where A LIKE ='aa[0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]'


热门排行

今日推荐

热门手游