Heim > Backend-Entwicklung > PHP-Tutorial > 统计某个连续时间段内的用户具体数据的sql语句该如何写?

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

WBOY
Freigeben: 2016-06-06 20:28:51
Original
1760 Leute haben es durchsucht

数据库中的测试数据如下所示,
统计某个连续时间段内的用户具体数据的sql语句该如何写?

想要统计某个时间段内的用户来与没来的情况,最终展示结果如下所示:
统计某个连续时间段内的用户具体数据的sql语句该如何写?

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');

回复内容:

数据库中的测试数据如下所示,
统计某个连续时间段内的用户具体数据的sql语句该如何写?

想要统计某个时间段内的用户来与没来的情况,最终展示结果如下所示:
统计某个连续时间段内的用户具体数据的sql语句该如何写?

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;
统计某个连续时间段内的用户具体数据的sql语句该如何写?

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage