MySQL用表实现号池的功能
最近单位做IM聊天群其中一个功能是新建群,获取群号.群号是我们运营定义的。好的号码要留着卖吧.模拟群号的号池.create table test( id int primary key, state int)engine =innodb;insert into test values(1,0),(2,0),(3,0),(4,0),(5,0),(6,0),(7,0),(8...
防跳号的两种方式
在财务上有一种需求,就是单据号必须连续不能中断.这种需求用Oracle的序列不能实现,MySQL的auto_increment也不能实现.因为事务回滚,或者实例重启,都可能造成单据号不连续的情况,就是跳号.防止跳号有两种方式,但是前提都是不能删除数据.1.用锁限制并发.一个表有两个字段,一个是模块名称,一个是当前模块的序列值create ...
SQL星期数问题
MySQL技术内幕 SQL编程 48页其中讨论到一些时间日期的问题,非常有意思创建2015年1月的日期数据.create table testselect str_to_date('2014-12-27','%Y-%m-%d')+interval id day as "dt" from nums where id
MySQL连续范围问题
MySQL技术内幕 SQL编程 58页首先创建实验表create table t(a int primary key);insert into t values(1);insert into t values(2);insert into t values(3);insert into t values(100);insert into t values(101);insert into t values(103);insert into t values(104);insert into t values(105);commit;实验...
行转列计算差值的一种优化
http://blog.itpub.net/29254281/viewspace-1379159/前文在计算差值的时候使用的是union all先通过左连接计算两天签到人数的差值,然后union all当天签到的用户,最后行换列但是这种方式的效率很低,需要扫描两次表.(前文的数据和本文不一致,下面是本文使用的数据)受到where in list的启发,使用如下方式,可以仅扫描一...
行转列的应用
快到圣诞节了,需求弄出了一堆活动其中一个活动要求用户每天签到,连续若干天,有不同级别的奖励.签到表模拟如下,userid记录用户的ID,createtime模拟用户签到的时间.其中nums是数字辅助表,用法参见http://blog.itpub.net/29254281/viewspace-1362897/create table award_chance_history(&...
SQL Where in list 问题
ITPUB技术丛书 剑破冰山 170页Where in list问题还是挺常见的。前台可能传过来一系列参数,JAVA可能将其拼接在In列表里面.比较典型的场景是前台页面有一堆复选框,系统需要显示用户勾选的数据这时,后台很可能将其拼接为一个包含in列表的SQL不过,这种做法有两个缺陷1.Oracle In列表的数目有限制(1000)2.不能复用执行计...
MySQL年龄日期问题
前几天接到一个需求,要根据用户的出生日期算出他的年龄.模拟数据如下,使用了数字辅助表创建数据.http://blog.itpub.net/29254281/viewspace-1362897/drop table if exists t;create table t( id int primary key auto_increment, birthday datetime );trun...
MySQL数字辅助表
最近在做一个活动签到的功能,每个用户每天签到,累计到一定次数,可以换一些奖品。签到表的设计如下CREATE TABLE `award_chance_history` ( `id` int(11) NOT NULL AUTO_INCREMENT, `awardActId` int(11) DEFAULT NULL COMMENT '活动id', `vvi...
MySQL删除重复数据
drop table if exists test ;create table test( id int primary key auto_increment, name varchar(10) );insert into test(name) values('1');insert into test(name) values('2');insert into test(name) values('3');insert into test(name) values('4');insert into test(name) value...
MySQL使用自定义变量模拟分析函数
初始化实验结构和数据create table test( tid varchar(32) primary key, stat int not null, createtime timestamp not null);INSERT INTO test (tid,stat,createtime) VALUES ('ac55...
使用Oracle分析函数随机抽取N条记录
模拟一个歌曲表,有歌曲ID和作者ID并且初始化一些数据create table music( music_id int primary key, userid int);insert into music(music_id)select rownum rn from dual connect by level
MySQL自定义变量处理行号问题
今天遇到一个业务性很强的问题。应该不是很通用。实验数据初始化:create table test( id int primary key , Priority int not null);insert into test values(30,1),(31,1),(32,1),(33,2),(34,3...
SQL根据分组求连续范围
http://www.itpub.net/thread-1873736-1-1.htmlcreate table test (id int,name varchar(10));insert into test values(11,'aaa');insert into test values(12,'aaa');insert into test values(13,'aaa');insert into test values(15,'aaa');insert into t...
MySQL实现差集(Minus)和交集(Intersect)
MySQL没有实现Minus和Intersect功能,就像它也没有实现cube的功能一样。可以用SQL实现同样的功能,就是麻烦了点。drop table t1;drop table t2;create table t1(id int primary key,nickname varchar(20),playNum varchar(20));create table t2(id int primary key,nickname var...
使用查询结果更新表的方法
使用一个查询的结果更新另一个表的数据。模拟实验如下,使用t2表的查询结果,更新t1表相同id的数据。drop table t1;drop table t2;create table t1(id int primary key,nickname varchar(20),playNum varchar(20));create table t2(id int primary key,nickname varchar(20),play...
MySQL的Group By分组
一直以来,还是Oracle用的比较多。MySQL的分组,看着总是有些奇怪。实验如下,创建初始化数据:create table sod_artist_category_relation( songid int , CategoryID int);insert into sod_artist_category_relation v...
SQL乘法表
一段SQL想起一段在后勤的往事物是人非打印9*9乘法表witht as( select level as n from dual connect by level
取每个分组的前若干名
以Oracle hr模式的employees表为例。查询每个部门收入最多的前三个人。实验数据初始化:create table test as select * from hr.employees;方法一:select department_id,last_name,salary from test t1 where ( ...
一道SQL题
王工出的一道SQL题数据初始化:create table t(a varchar2(10));insert into t values('b');insert into t values(null);insert into t values(null);insert into t values('a');insert into t values('e');insert into t values(null);insert into t values('c');insert into t values('d');commit;要求:给每...