統計某個連續時間段內的使用者具體資料的sql語句該如何寫?

來源:互聯網
上載者:User

資料庫中的測試資料如下所示,

想要統計某個時間段內的使用者來與沒來的情況,最終展示結果如下所示:

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測試語句:

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;

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.