使用 groupby 将值从另一个 table 插入特定列

Insert value into specific column from another table with groupby

我正在使用 MySQL。我想将日期时间的 groupby 的值结果插入到特定列(可能使用 where)。让我们说: 我有两个 tables (a, b)。在 table a 中,我想获取一个小时内的总记录数(我有 datetime 列),然后结果将插入到 table b 中,但在特定 ID 中(已经存在 ID 的值)。

这是我的错误代码:

INSERT INTO b(value)
WHERE ID=15
SELECT DAY COUNT(*)
FROM a
WHERE date >= '2015-09-19 00:00:00' AND date < '2015-09-19 00:59:59'
GROUP BY DAY(date),HOUR(date);";

我可以从这个案例中查询吗? 非常感谢您的回复!

架构

create table tA
(   id int auto_increment primary key,
    theDate datetime not null,
    -- other stuff
    key(theDate) -- make it snappy fast
);

create table tB
(   myId int primary key,   -- by definition PK is not null
    someCol int not null
);

-- truncate table tA;
-- truncate table tB;

insert tA(theDate) values
('2015-09-19'),
('2015-09-19 00:24:21'),
('2015-09-19 07:24:21'),
('2015-09-20 00:00:00');

insert tB(myId,someCol) values (15,-1); --    (-1) just for the heck of it
insert tB(myId,someCol) values (16,-1); --    (-1) just for the heck of it

查询

update tB
set someCol=(select count(*) from tA where theDate between '2015-09-19 00:00:00' and '2015-09-19 00:59:59')
where tB.myId=15;

结果

select * from tB;
+------+---------+
| myId | someCol |
+------+---------+
|   15 |       2 |
|   16 |      -1 |
+------+---------+

只有myId=15被触及