我尝试使用此命令将tblDoXang中的所有记录插入到满足条件的tblDoXang1中:
insert into gtse.tblDoXang1
SELECT *
FROM `gtse`.`tblDoXang`
where concat(date(from_unixtime(thoiGian)), hour(from_unixtime(thoiGian)), nhienLieu) in
(select
concat(date(from_unixtime(thoiGian)), hour(from_unixtime(thoiGian)), max(nhienLieu))
from gtse.tblDoXang
-- where accountID = 'anhtuaniphone' and deviceID = '14C-10152'
-- and from_unixtime(thoiGian) between '2014-11-01 00:00:02' and '2014-11-17 23:59:59'
group by
date(from_unixtime(thoiGian)),
hour(from_unixtime(thoiGian)));
它向我显示了消息:7432 row(s) affected
。之后,我使用此查询检查新表(tblDoXang1)中的数据:
SELECT from_unixtime(thoiGian), nhienLieu
FROM `gtse`.`tblDoXang1`
然后我看到:7432 row(s) returned
。我还检查了旧表(tblDoXang)的条件:
SELECT *
FROM `gtse`.`tblDoXang`
where concat(date(from_unixtime(thoiGian)), hour(from_unixtime(thoiGian)), nhienLieu) in
(select
concat(date(from_unixtime(thoiGian)), hour(from_unixtime(thoiGian)), max(nhienLieu))
from gtse.tblDoXang
-- where accountID = 'anhtuaniphone' and deviceID = '14C-10152'
-- and from_unixtime(thoiGian) between '2014-11-01 00:00:02' and '2014-11-17 23:59:59'
group by
date(from_unixtime(thoiGian)),
hour(from_unixtime(thoiGian)));
它也返回了7432行。但是,当我使用特定值进行测试时,我收到了两个不同的结果(我预计它们是相同的)。此查询返回了两条记录(我从新表中选择):
select from_unixtime(thoiGian), nhienLieu
from gtse.tblDoXang1
where
(from_unixtime(thoiGian) between '2014-10-01 00:00:02' and '2014-11-18 23:59:59')
and accountID = 'vinhnghia'
and deviceID = '14C-00263'
虽然这会抛出5条记录(条件相同)(我从旧表中选择我在上面第一个查询中插入的条件):
SELECT *
FROM `gtse`.`tblDoXang`
where concat(date(from_unixtime(thoiGian)), hour(from_unixtime(thoiGian)), nhienLieu) in
(select
concat(date(from_unixtime(thoiGian)), hour(from_unixtime(thoiGian)), max(nhienLieu))
from gtse.tblDoXang
where accountID = 'vingnghia' and deviceID = '14C-00263'
and from_unixtime(thoiGian) between '2014-10-01 00:00:02' and '2014-11-18 23:59:59'
group by
date(from_unixtime(thoiGian)),
hour(from_unixtime(thoiGian)));
我对这个结果感到困惑,我不确切地知道我错在哪里,所以有人可以给我一些建议吗?提前谢谢。
答案 0 :(得分:2)
我发现了自己的错误。在此查询中:
SELECT *
FROM `gtse`.`tblDoXang`
where concat(date(from_unixtime(thoiGian)), hour(from_unixtime(thoiGian)), nhienLieu) in
(select
concat(date(from_unixtime(thoiGian)), hour(from_unixtime(thoiGian)), max(nhienLieu))
from gtse.tblDoXang
-- where accountID = 'anhtuaniphone' and deviceID = '14C-10152'
-- and from_unixtime(thoiGian) between '2014-11-01 00:00:02' and '2014-11-17 23:59:59'
group by
date(from_unixtime(thoiGian)),
hour(from_unixtime(thoiGian)));
我没有指定时间,帐户ID和设备ID。它与以下不同:
SELECT *
FROM `gtse`.`tblDoXang`
where concat(date(from_unixtime(thoiGian)), hour(from_unixtime(thoiGian)), nhienLieu) in
(select
concat(date(from_unixtime(thoiGian)), hour(from_unixtime(thoiGian)), max(nhienLieu))
from gtse.tblDoXang
where accountID = 'vingnghia' and deviceID = '14C-00263'
and from_unixtime(thoiGian) between '2014-10-01 00:00:02' and '2014-11-18 23:59:59'
group by
date(from_unixtime(thoiGian)),
hour(from_unixtime(thoiGian)));
我具体在哪里条件。所以查询:
select from_unixtime(thoiGian), nhienLieu
from gtse.tblDoXang1
where
(from_unixtime(thoiGian) between '2014-10-01 00:00:02' and '2014-11-18 23:59:59')
and accountID = 'vinhnghia'
and deviceID = '14C-00263'
对应于此:
SELECT from_unixtime(thoiGian), nhienLieu
FROM (
SELECT *
FROM `gtse`.`tblDoXang`
where concat(date(from_unixtime(thoiGian)), hour(from_unixtime(thoiGian)), nhienLieu) in
(select
concat(date(from_unixtime(thoiGian)), hour(from_unixtime(thoiGian)), max(nhienLieu))
from gtse.tblDoXang
group by
date(from_unixtime(thoiGian)),
hour(from_unixtime(thoiGian)))
) as t
where accountID = 'vinhnghia'
and deviceID = '14C-00263'
and from_unixtime(thoiGian) between '2014-10-01 00:00:00' and '2014-11-18 23:59:59'
order by thoiGian asc;
这就是我从上面的两个查询得到不同结果的原因。
答案 1 :(得分:1)
你的想法并不好。请想象一下,您总共有十个帐户。有了这个查询:
SELECT *
FROM `gtse`.`tblDoXang`
where concat(date(from_unixtime(thoiGian)), hour(from_unixtime(thoiGian)), nhienLieu) in
(select
concat(date(from_unixtime(thoiGian)), hour(from_unixtime(thoiGian)), max(nhienLieu))
from gtse.tblDoXang
-- where accountID = 'anhtuaniphone' and deviceID = '14C-10152'
-- and from_unixtime(thoiGian) between '2014-11-01 00:00:02' and '2014-11-17 23:59:59'
group by
date(from_unixtime(thoiGian)),
hour(from_unixtime(thoiGian)));
您只会在十个帐户的每一小时内收到最大值(nhienLieu
),而不是一个帐户(您预期的帐户)。所以这就是为什么结果是两种情况之间的差异的原因:当你特别指定accountID,deviceID和thoiGian以及你在表中选择all时。我认为这个查询将满足你的目标:
SELECT *
FROM `gtse`.`tblDoXang`
where concat(date(from_unixtime(thoiGian)), hour(from_unixtime(thoiGian)), nhienLieu) in
(select
concat(date(from_unixtime(thoiGian)), hour(from_unixtime(thoiGian)), max(nhienLieu))
from gtse.tblDoXang
-- where accountID = 'anhtuaniphone' and deviceID = '14C-10152'
-- and from_unixtime(thoiGian) between '2014-11-01 00:00:02' and '2014-11-17 23:59:59'
group by
date(from_unixtime(thoiGian)),
hour(from_unixtime(thoiGian)),
accountID,deviceID);
如果你得到的话,请尝试并告诉我任何问题。