各位高手请教了,两个SQL的问题

zhouxin82 2007-01-26 05:55:27
1.下面为user表的脚本:
DROP TABLE IF EXISTS user;
CREATE TABLE IF NOT EXISTS user (
mobile INT UNSIGNED NOT NULL, --手机号码
app_date INT UNSIGNED NOT NULL, --加入日期
ban_date INT UNSIGNED NOT NULL, --用户关闭/取消日期
pay_date INT UNSIGNED NOT NULL, --成功扣费日期,用于标志当月扣费时间,防止重复扣费等问题
--免费用户,=0表示正常
--收费用户,=0表示未扣
stat TINYINT UNSIGNED NOT NULL, --用户状态
--0—0组;1—1组;
--2--关闭组

PRIMARY KEY (mobile),
INDEX app_date (app_date),
INDEX ban_date (ban_date),
INDEX stat (stat)
) TYPE=MyISAM;

现根据user表做一个销户处理。下表为销户表,现假设此表数据已经导入,脚本为:
DROP TABLE IF EXISTS user_off;
CREATE TABLE IF NOT EXISTS user_off (
mobile INT UNSIGNED NOT NULL, --手机号码
PRIMARY KEY (mobile)
) TYPE=MyISAM;

1.1 首先每次进行销户处理的时候需要统计好销户用户在用户表中的分布情况,以便做销户报表。请写出销户用户在我们各组(stat)中的分布情况的SQL查询语句,如1组待销户多少人,关闭组待销户多少人等。(由一个语句完成)



1.2 统计完销户分布情况后,然后对用户在用户表中的状态进行处理。将用户在user表中的状态(stat)改为关闭组(stat=2)的标志,另外用户的加入时间要改为0,取消的时间改为销户处理的时间(20060519)。请写出处理的SQL语句。
...全文
285 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
adetwo 2008-05-28
  • 打赏
  • 举报
回复
看晕了
utpcb 2008-05-28
  • 打赏
  • 举报
回复
MARK 很长
ChinaJiaBing 2008-05-28
  • 打赏
  • 举报
回复
具体数据....

case when
-狙击手- 2008-05-27
  • 打赏
  • 举报
回复
。。。
Herb2 2008-05-19
  • 打赏
  • 举报
回复
xhc_ozh 2007-01-27
  • 打赏
  • 举报
回复
-- create table
CREATE TABLE [user] (
mobile INT NOT NULL,--手机号码
app_date INT NOT NULL,--加入日期
ban_date INT NOT NULL,--用户关闭/取消日期
pay_date INT NOT NULL,--成功扣费日期,用于标志当月扣费时间,防止重复扣费等问题
--免费用户,=0表示正常
--收费用户,=0表示未扣
stat TINYINT NOT NULL,--用户状态
--0—0组;1—1组;
--2--关闭组
PRIMARY KEY (mobile),

)

CREATE TABLE user_off (
mobile INT NOT NULL,--手机号码
PRIMARY KEY (mobile)
);

-- test data
insert [user] select '1234567890', '0', '20070101', '20070101', '0' union
select '1234567891', '20070101', '0', '20070101', '0' union
select '1234567892', '20070101', '0', '20070101', '0' union
select '1234567893', '20070101', '0', '20070101', '1' union
select '1234567894', '20070101', '0', '20070101', '1' union
select '1234567895', '20070101', '0', '20070101', '1' union
select '1234567896', '0', '20070101', '20070101', '2' union
select '1234567897', '0', '20070101', '20070101', '2' union
select '1234567898', '0', '20070101', '20070101', '2'

insert user_off select '1234567895' union
select '1234567894' union
select '1234567892' union
select '1234567898' union
select '1234567890'

-- 1
select case when b.stat=0 then '0组' when b.stat=1 then '1组' when b.stat=2 then '关闭组' end '分组', count(*) '人数'
from user_off a, [user] b
where a.mobile=b.mobile
group by b.stat

-- 2
update [user] set app_date=0, ban_date=replace(left(convert(varchar, getdate(),120),10), '-','')
from user_off a, [user] b where a.mobile=b.mobile

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧