‘壹’ sql根据某一个字段重复只取第一条数据
代码如下:
select * from tbl_DPImg where ID in (select min(ID) from tbl_DPImg group by DPID)
处理后结果为:
查找表中多余的重复记录,重复记录是根据单个字段(teamId)来判断
select * from team where teamId in (select teamId from team group by teamId having count(teamId) > 1)
删除表中多余的重复记录,重复记录是根据单个字段(teamId)来判断,只留有rowid最小的记录
delete from team where
teamName in(select teamName from team group by teamName having count(teamName) > 1)
and teamId not in (select min(teamId) from team group by teamName having count(teamName)>1)
(1)sql只取关键字之前的数据扩展阅读
数据记录筛选:
sql="select * from 数据表 where字段名=字段值 order by字段名[desc]"(按某个字段值降序排列。默认升序ASC)
sql="select * from 数据表 where字段名like '%字段值%' order by 字段名 [desc]"
sql="select top 10 * from 数据表 where字段名=字段值 order by 字段名 [desc]"
sql="select top 10 * from 数据表 order by 字段名 [desc]"
sql="select * from 数据表 where字段名in ('值1','值2','值3')"
sql="select * from 数据表 where字段名between 值1 and 值2"
‘贰’ sql如何截取字符串中前面的数字
如果提取字符串中的数字,需要自定义函数,以下函数包含截取字符串中的数字、字母、汉字等。
createfunctionfn_myget
(@strasvarchar(max),
@categoryasvarchar(10)='Chinese',
@startasint=1,
@endasint=100000)
returnsvarchar(max)
as
begin
declare@iint=@start
declare@lenstrint=len(@str)
declare@str1varchar(max)
declare@strrevarchar(max)=''
while@i<=@lenstrand@i<=@end
begin
set@str1=substring(@str,@i,1);
--提取汉字
if(@Category='Chinese')
begin
if(len(@str1)!=datalength(@str1))
set@strre=@strre+@str1
end
--提取字母
elseif(@Category='Letters')
begin
if((ascii(@str1)>=65andascii(@str1)<=90orascii(@str1)>=97andascii(@str1)<=122))
set@strre=@strre+@str1
end
--提取数字
elseif(@Category='Digital')
begin
if(ascii(@str1)>=48andascii(@str1)<=57)
set@strre=@strre+@str1
end
else
set@strre='输入错误'
set@i=@i+1
end
return(@strre)
end
测试:
1、截取字符串dsajf23423423中的数字
selectdbo.fn_myget('dsajf23423423','Digital',1,1000)
结果:
自定义函数说明:
函数共需要4个参数:
第一个参数是要截取的字符串;
第二个参数根据要截取的内容不同,分别有“Digital”(截取数字)、“Letters”(截取字母)、“Chinese”(截取中文);
第三个参数代表截取的起始位;
第四个参数代表截取的截取的结束位。
‘叁’ sql 截取某个字符之前的数据
CREATE PROCEDURE pim_Utility_ReadCRMSaleLead
AS
select A.ProjectName as '项目名称', A.SaleDistrict as '项目所在地',A.Address as '客户地址', SUBSTRING(1,CHARINDEX(‘+’,A.RelateLead1)- 1,LEN(A.RelateLead1)) as '设计院',
A.BudgetNumber as '预计容量',A.Finish_On as '订货时间' ,
A.WorkStatus as '状态', B.FolderId
INTO #Temp0 from pimCRMSaleLead A ,pimCRMLead B
where A.CustomerId *= B.LeadID
update #Temp0 set #Temp0.项目所在地=C.Name FROM pimDataClassFolder C WHERE #Temp0.FolderId=C.FolderId
SELECT * FROM #Temp0
GO
‘肆’ 如何截取一段sql中某个字符串之前的内容,在线等
/****** Sql Server中截取字符串的常用方法 ******/--1、LEFT()方法-----函数说明-----1)语法:LEFT(character,integer) --2)介绍:参数1:要截取的字符串,参数2:截取字符个数--3)使用:--返回从字符串左边开始指定个数的字符--select LEFT('SqlServer_2008',3)--4)返回:Sql--1、RIGHT()方法----- right()函数说明-----1)语法:RIGHT(character,integer) --2)介绍:参数1:要截取的字符串,参数2:截取字符个数--3)使用:--返回从字符串右边开始指定个数的字符--select LEFT('SqlServer_2008',4)--4)返回:2008--1、SUBSTRING()方法----- substring()函数说明-----1)语法:SUBSTRING(character,start,length) --2)介绍:参数1:要截取的字符串,参数2:开始截取的下标,参数3:截取的字符长度--3)使用:--返回从字符串中间的字符--select SUBSTRING('SqlServer_2008',4,6)--4)返回:Server
‘伍’ sql语句,根据关键字,取第一条数据
select No,Count,max(Time) as Time
from 表
group by No,Count
‘陆’ sql取某个字符前面、后面的内容
select substr('338*304/100*100',1,instr('338*304/100*100','/')-1) from al
--338*304
select substr('338*304/100*100',instr('338*304/100*100','/')+1) from al
--100*100
将单引号里面的内容替换即可,我用的是oracle,你下次提问最好要说明是什么数据库
‘柒’ SQL语句怎么取当前时间之前的数据
--意思就是先排除今天以后的数据然后倒序,只取前面10条数据
selecttop10.*fromtablewhereconvert(char(10),datetime,120)<convert(char(10),GETDATE(),120)orderbydatetimedesc
‘捌’ sql 查询出结果 怎么让它只显示前十条数据
sql 查询出结果让它只显示前十条数据可以通过top关键字。语句格式为SELECT TOP 10 <列名表> FROM <表名> [查询条件]。TOP关键字在SQL语言中用来限制返回结果集中的记录条数,有两种使用形式,其中一种是TOP关键字后带数字,表示要返回结果集中的记录条数。
(8)sql只取关键字之前的数据扩展阅读:
TOP关键字的另一种使用形式,是TOP关键字后带百分比数,表示要返回结果集中指定百分比的记录数。语法格式为SELECT TOP n PERCENT <列名表> FROM <表名> [查询条件]。
TOP关键字对于拥有数千条记录的大型表来说,是非常有用的。在具体使用过程中,也可以结合条件子句和排序子句(如何进行排序)等实现较为丰富的功能。注意的是,并非所有的数据库系统都支持 TOP 关键字。
‘玖’ sql如何取多个重复关键字前的某几个字
从第一个开始的话 SELECT substring(@nr,1,charindex('院',@nr)-1) as idx
substring( 字符串,开始位置,所取长度)
‘拾’ oracle中 sql截取某个字符前面和后面的值
1、将新建好的表aaaa,填充需要查询的数据以"_"或者","为分隔符的两条数据。