php统计一周内的sql语句,统计某个连续时间段内的用户具体数据的sql语句该如何写?...

该博客展示了如何使用SQL语句统计特定时间段内用户来访记录,通过创建数据库表并插入测试数据,然后利用SELECT语句结合GROUP_CONCAT和SUM函数,实现用户每日访问情况的统计。内容涉及数据库操作和SQL查询技巧。
摘要由CSDN通过智能技术生成

数据库中的测试数据如下所示,

bVmU5V

想要统计某个时间段内的用户来与没来的情况,最终展示结果如下所示:

bVmU5T

SQL测试语句:

CREATE TABLE test (

id int(11) NOT NULL AUTO_INCREMENT,

come_date date NOT NULL COMMENT '日期',

username varchar(255) NOT NULL,

PRIMARY KEY (id),

KEY username (username)

) ENGINE=MyISAM AUTO_INCREMENT=25 DEFAULT CHARSET=utf8

INSERT INTO test VALUES (1, '2015-7-1', 'test_1');

INSERT INTO test VALUES (2, '2015-7-1', 'test_2');

INSERT INTO test VALUES (3, '2015-7-1', 'test_3');

INSERT INTO test VALUES (4, '2015-7-2', 'test_1');

INSERT INTO test VALUES (5, '2015-7-2', 'test_2');

INSERT INTO test VALUES (6, '2015-7-2', 'test_3');

INSERT INTO test VALUES (7, '2015-7-5', 'test_1');

INSERT INTO test VALUES (8, '2015-7-5', 'test_2');

INSERT INTO test VALUES (9, '2015-7-5', 'test_3');

INSERT INTO test VALUES (10, '2015-7-5', 'test_4');

INSERT INTO test VALUES (11, '2015-7-7', 'test_2');

INSERT INTO test VALUES (12, '2015-7-7', 'test_3');

INSERT INTO test VALUES (13, '2015-7-7', 'test_6');

INSERT INTO test VALUES (14, '2015-7-7', 'test_7');

INSERT INTO test VALUES (15, '2015-7-7', 'test_8');

INSERT INTO test VALUES (16, '2015-7-7', 'test_9');

INSERT INTO test VALUES (17, '2015-7-7', 'test_10');

INSERT INTO test VALUES (18, '2015-7-7', 'test_11');

INSERT INTO test VALUES (19, '2015-7-8', 'test_2');

INSERT INTO test VALUES (20, '2015-7-8', 'test_3');

INSERT INTO test VALUES (21, '2015-7-8', 'test_10');

INSERT INTO test VALUES (22, '2015-7-8', 'test_11');

INSERT INTO test VALUES (23, '2015-7-9', 'test_1');

INSERT INTO test VALUES (24, '2015-7-9', 'test_10');

回复内容:

数据库中的测试数据如下所示,

bVmU5V

想要统计某个时间段内的用户来与没来的情况,最终展示结果如下所示:

bVmU5T

SQL测试语句:

CREATE TABLE test (

id int(11) NOT NULL AUTO_INCREMENT,

come_date date NOT NULL COMMENT '日期',

username varchar(255) NOT NULL,

PRIMARY KEY (id),

KEY username (username)

) ENGINE=MyISAM AUTO_INCREMENT=25 DEFAULT CHARSET=utf8

INSERT INTO test VALUES (1, '2015-7-1', 'test_1');

INSERT INTO test VALUES (2, '2015-7-1', 'test_2');

INSERT INTO test VALUES (3, '2015-7-1', 'test_3');

INSERT INTO test VALUES (4, '2015-7-2', 'test_1');

INSERT INTO test VALUES (5, '2015-7-2', 'test_2');

INSERT INTO test VALUES (6, '2015-7-2', 'test_3');

INSERT INTO test VALUES (7, '2015-7-5', 'test_1');

INSERT INTO test VALUES (8, '2015-7-5', 'test_2');

INSERT INTO test VALUES (9, '2015-7-5', 'test_3');

INSERT INTO test VALUES (10, '2015-7-5', 'test_4');

INSERT INTO test VALUES (11, '2015-7-7', 'test_2');

INSERT INTO test VALUES (12, '2015-7-7', 'test_3');

INSERT INTO test VALUES (13, '2015-7-7', 'test_6');

INSERT INTO test VALUES (14, '2015-7-7', 'test_7');

INSERT INTO test VALUES (15, '2015-7-7', 'test_8');

INSERT INTO test VALUES (16, '2015-7-7', 'test_9');

INSERT INTO test VALUES (17, '2015-7-7', 'test_10');

INSERT INTO test VALUES (18, '2015-7-7', 'test_11');

INSERT INTO test VALUES (19, '2015-7-8', 'test_2');

INSERT INTO test VALUES (20, '2015-7-8', 'test_3');

INSERT INTO test VALUES (21, '2015-7-8', 'test_10');

INSERT INTO test VALUES (22, '2015-7-8', 'test_11');

INSERT INTO test VALUES (23, '2015-7-9', 'test_1');

INSERT INTO test VALUES (24, '2015-7-9', 'test_10');

这里面有里想要的答案:

http://blog.itpub.net/29254281/viewspace-1385638

这个是必须要以数据库表的形式展现吗?如果没必要的话 可以select * from from test where come_date between '2015-07-01' and '2015-07-09' 对数据取出后 用后台语言处理 比较好做

如果必须要以sql查出所需结果,select aa.username,group_concat(DAY(come_date)),sum(1) from (select come_date,username from test where come_date between '2015-07-01' and '2015-07-09') aa group by aa.username;

bVpGhW

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值