我有一个使用H2作为内存数据库的测试。下表,数据和查询在MySQL中生成具有预期值的单行,但不在H2中生成:
CREATE TABLE redirects (
site_id INTEGER NOT NULL,
company_id INTEGER NOT NULL,
type CHAR(1) NOT NULL,
capture TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO redirects (site_id, company_id, type, capture) VALUES (1, 1, 'm', CURRENT_TIMESTAMP);
INSERT INTO redirects (site_id, company_id, type, capture) VALUES (1, 2, 'm', CURRENT_TIMESTAMP);
INSERT INTO redirects (site_id, company_id, type, capture) VALUES (1, 1, 'h', CURRENT_TIMESTAMP);
INSERT INTO redirects (site_id, company_id, type, capture) VALUES (1, 1, 'h', CURRENT_TIMESTAMP);
INSERT INTO redirects (site_id, company_id, type, capture) VALUES (1, 2, 'h', CURRENT_TIMESTAMP);
INSERT INTO redirects (site_id, company_id, type, capture) VALUES (1, 1, 'a', CURRENT_TIMESTAMP);
INSERT INTO redirects (site_id, company_id, type, capture) VALUES (1, 1, 'a', CURRENT_TIMESTAMP);
INSERT INTO redirects (site_id, company_id, type, capture) VALUES (1, 1, 'a', CURRENT_TIMESTAMP);
INSERT INTO redirects (site_id, company_id, type, capture) VALUES (1, 2, 'a', CURRENT_TIMESTAMP);
SELECT
COALESCE(site_id, 1) site_id,
COALESCE(company_id, -1) company_id,
COUNT(CASE WHEN type = 'm' THEN 1 END) views,
COUNT(CASE WHEN type IN ('h', 'a') THEN 1 END) clicks
FROM
redirects
WHERE
(site_id = 1 AND company_id = 3 AND capture > TIMESTAMP '2017-04-24 00:00:00.0');
MySQL中的结果如下:
+---------+------------+-------+--------+
| site_id | company_id | views | clicks |
+---------+------------+-------+--------+
| 1 | -1 | 0 | 0 |
+---------+------------+-------+--------+
但是,在H2(版本1.4.194)中,结果是:
SITE_ID COMPANY_ID VIEWS CLICKS
1 2 0 0