MySQL中的多表插入更新与MS-SQL的对比
    
            
摘要:MySQL多表插入:INSERT INTO tdb_goods_cates (cate_name) SELECT goods_cate FROM tdb_goods GROUP BY goods_cate;--INSERT INTO 表名 (列名)查询对应的列MS SQL 多表插入: 与MySQL...
        
阅读全文
 
            
                posted @ 
2015-02-12 16:22 
LitDev
        
阅读(1281) 
         
        推荐(0)     
             
         
            
            
    SQL 时间及字符串操作
    
            
摘要:都是一些很基础很常用的,在这里记录一下获取年月日: year(时间) ---获取年,2014 month(时间) ----获取月,5 day(时间) -----获取天,6如果月份或日期不足两位数,则前面补0,如5月,补0为:05,处理方法是使用right('要补的字符串'+原字符串,不足几位就...
        
阅读全文
 
            
                posted @ 
2014-12-04 17:15 
LitDev
        
阅读(926) 
         
        推荐(0)     
             
         
            
            
    MS SQL 取分组后的几条数据
    
            
摘要:SELECT uploaddate ,ptnumber ,instcount FROM ( SELECT ROW_NUMBER() OVER( PARTITION BY uploaddate ...
        
阅读全文
 
            
                posted @ 
2014-10-22 10:01 
LitDev
        
阅读(518) 
         
        推荐(0)     
             
         
            
            
    MS SQL update set select
    
            
摘要:有张表a,已经有数据再有张表b,也已查询出数据两张表有外键关联需求如下: 更新表a中的某个字段,这个字段要加上(都是int型的数据)对应表b中的数据作为更新的最终数据update #libList set actiCount=ISNULL(actiCount,0)+ ISNULL(total,0...
        
阅读全文
 
            
                posted @ 
2014-10-22 09:58 
LitDev
        
阅读(977) 
         
        推荐(0)     
             
         
            
            
    记一次SQL xml字段关联查询
    
            
摘要:需求: 一张表是APP表,结构如下: app_category为该游戏所属的类别ID,xml字段类型 另一张表是类别表,就ID对应名称,这就不上图了。 还有一张表是每个游戏的下载记录,结构如下: DownLogs_APPId为对应的游戏 那么需求来了,要查询游戏的下载记录...
        
阅读全文
 
            
                posted @ 
2014-10-20 11:47 
LitDev
        
阅读(566) 
         
        推荐(0)     
             
         
            
            
    MS-SQL循环、随机数
    
            
摘要:---创建视图create view myview as select re=rand() --自定义函数:取得指定范围的随机数 create function mydata( @a int, @b int) returns decimal(38,0) as begin declare @r dec...
        
阅读全文
 
            
                posted @ 
2014-09-15 16:14 
LitDev
        
阅读(319) 
         
        推荐(0)     
             
         
            
            
    MS-SQL使用xp_cmdshell命令导出数据到excel
    
            
摘要:exec master..xp_cmdshell 'bcp "select c.Category_Title as 标题,p.Category_Title as 所属分类 from ltblGameStore..tbl_Category as c left join ltblGameStore..t...
        
阅读全文
 
            
                posted @ 
2014-09-15 13:27 
LitDev
        
阅读(1777) 
         
        推荐(0)     
             
         
            
            
    SQL一对多特殊查询,取唯一一条
    
            
摘要:主表:辅表:一个app对应多个apk,现在要取上线(Apk_Status最大的)的应用select * from [dbo].[tbl_APP] as app join (select * from [dbo].[tbl_Apk] as AA where not exists(select top ...
        
阅读全文
 
            
                posted @ 
2014-07-24 21:46 
LitDev
        
阅读(4803) 
         
        推荐(0)     
             
         
            
            
    T-SQL操作XML 数据类型方法 "modify" 的参数 1 必须是字符串文字。
    
            
摘要:----删除关键字的同时也清理AP表中所有关联这个ID的数据create trigger Trg_UpdateAppWordOnDelKeyWordon [dbo].[tbl_KeyWord]for deleteasdeclare @work_id intselect @work_id = Word...
        
阅读全文
 
            
                posted @ 
2014-07-21 14:25 
LitDev
        
阅读(968) 
         
        推荐(0)     
             
         
            
            
    MS-SQL中取用户连续签到的次数
    
            
摘要:表结构如下:每个用户每天只能签到一次现在前面的需求是判断某个用户在某天是否是连续签到,使用sql中的递归来实现with currentDateCTE AS ( -- 当前天. SELECT * FROM dt_Signin WHERE user_id = 270 and CONVERT(DATE, sign_time) = CONVERT(DATE, '2013-05-16 22:15:32.670')--这就是需求中的某天),prevDateCTE AS ( -- 向前递归. SELECT * FROM currentDateCTE UNION ALL SELECT prev
        
阅读全文
 
            
                posted @ 
2013-05-29 15:56 
LitDev
        
阅读(4779) 
         
        推荐(0)     
             
         
            
            
    MS_SQL模糊查询like和charindex的对比
    
            
摘要:like查询效率低下,网上搜了一下替代like查询的方法,都是说用charindex方法,自己对比了一下查询速度test1表中有一千两百多万条数据,我只给ID加了索引先看一下 '%我%'这种模糊查询:declare @q datetimeset @q = getdate()select ID,U_Name,U_Sex,U_Age,U_Address from test1 where U_Name like '%我%'select [like执行花费时间(毫秒)]=datediff(ms,@q,getdate())declare @w datetimeset @w
        
阅读全文
 
            
                posted @ 
2012-11-28 23:01 
LitDev
        
阅读(12862) 
         
        推荐(4)     
             
         
            
            
    MS-SQL分页not in 方法改进之使用row_number
    
            
摘要:上一篇博客对比了not in 和 max\min分页的效率,这次来看看row_number分页效率如何在网上扒了一个row_number的分页存储过程,源地址:http://bbs.csdn.net/topics/300185125,在这里稍加修改,使之更加灵活create proc Proc_TablePage--表名@tablename nvarchar(20),--查询字段@selcolumn nvarchar(1000),--排序字段@sortcolumn nvarchar(255),--每页记录数@pagecount int,--页号@pageindex intasdeclar...
        
阅读全文
 
            
                posted @ 
2012-11-28 22:33 
LitDev
        
阅读(706) 
         
        推荐(0)     
             
         
            
            
    MS-SQL分页not in 方法改进之使用Max\Min
    
            
摘要:先看下表中共有多少条数据:一百二十多万条,呵呵。sql语句:declare @d datetimeset @d = getdate()select top 10 ID,U_Name,U_Age,U_Sex,U_Address from Test1 where ID not in (select top 9990 ID from Test1 order by ID) order by ID select [not in方法升序分页执行花费时间(毫秒)]=datediff(ms,@d,getdate()) declare @s datetimeset @s = getdate()select to
        
阅读全文
 
            
                posted @ 
2012-11-28 22:02 
LitDev
        
阅读(1285) 
         
        推荐(0)     
             
         
            
            
    SQL 循环
    
            
摘要:declare @i intset @i = 1while @i<100000beginexec Proc_InsertTest1 '测试使用数据',20,'男','地球村'set @i = @i+1end
        
阅读全文
 
            
                posted @ 
2012-11-19 18:29 
LitDev
        
阅读(195) 
         
        推荐(0)     
             
         
            
            
    MS-SQL  T-SQL语句查询最近一周的数据
    
            
摘要:T-SQL语句:select * from 表 where datediff(week,时间字段,getdate())=0语句中的时间字段就是表中的时间字段getdate(), 查询在当前时间一周内的数据拓展链接:http://yinzhihua2008.blog.163.com/blog/static/7943067200911534335298/
        
阅读全文
 
            
                posted @ 
2012-08-18 17:50 
LitDev
        
阅读(1371) 
         
        推荐(0)     
             
         
            
            
    MS-SQL中创建索引
    
            
摘要:create unique index 索引名称 on 表名 (字段名)
        
阅读全文
 
            
                posted @ 
2012-08-01 16:02 
LitDev
        
阅读(202) 
         
        推荐(0)     
             
         
            
            
    MS-SQL中创建约束
    
            
摘要:check --值约束alter table NewsMain add constraint check_State check(M_State in (0,1,2))unique--唯一约束create table 表名 (ID int , 要约束的字段名字 varchar(10) unique)或者:alter table 表名 add constraint 约束名称 unique(要约束字段的名称)示意:altertable NewsMain addconstraint unique_NewsId_NewsMain unique(NewsId)
        
阅读全文
 
            
                posted @ 
2012-07-30 15:32 
LitDev
        
阅读(272) 
         
        推荐(0)     
             
         
            
            
    使用存储过程更改MSSQL表中某一字段的名字
    
            
摘要:exec sp_rename 'test.dept_no' ,'dept_number', 'column'或exec sp_rename 'test.dept_no' ,'dept_number'test为表名,dept_no为要更改的字段,dept_number为更改后的字段
        
阅读全文
 
            
                posted @ 
2012-06-05 15:19 
LitDev
        
阅读(449) 
         
        推荐(0)     
             
         
            
            
    MS SQL 触发器
    
            
摘要:台湾佬为了防止乱更改数据,所以更改数据时要求把更改之前的的数据保存到另外一张表,也就是备份了,没有必要在项目中写,用触发器会简单很多创建 MSSQL更改型触发器(mssql2008下):create trigger UpdateSHInfo_bak on SHInfofor update asif update(S_LastUpdateTime) ---如果最后更新时间被更改了begininsert into SHInfo_bak (S_No,S_Name,S_Pwd,S_Sex,S_Birthday) select d.S_No,d.S_Name,d.S_Pwd,d.S_Sex,d.S_B.
        
阅读全文
 
            
                posted @ 
2012-05-31 11:34 
LitDev
        
阅读(488) 
         
        推荐(0)     
             
         
            
            
    MS SQL中复制一张表到另外一个数据库中一张表
    
            
摘要:--复制结构+数据select * into 数据库名.dbo.新表名 from 数据库名.dbo.原表名select * into Stockholder.dbo.SHInfo from dspring.dbo.HIREMEN--只复制结构 select * into 数据库名.dbo.新表名 from 数据库名.dbo.原表名 where 1=0后面的条件只要不成立就OK了 清空表中的数据:http://hi.baidu.com/angellys4/blog/item/c291c31676f2785e21a4e92b.html
        
阅读全文
 
            
                posted @ 
2012-05-22 11:09 
LitDev
        
阅读(2521) 
         
        推荐(0)