[英]Insert data from a table to another one in MySQL with where condition
I tried this command to insert all records from tblDoXang into tblDoXang1 which satisfy where condition: 我尝试使用此命令将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)));
It showed me the message: 7432 row(s) affected
. 它向我显示了以下消息:
7432 row(s) affected
。 After that, I check the data in new table(tblDoXang1) with this query: 之后,我使用此查询检查新表(tblDoXang1)中的数据:
SELECT from_unixtime(thoiGian), nhienLieu
FROM `gtse`.`tblDoXang1`
and then I saw: 7432 row(s) returned
. 然后我看到:
7432 row(s) returned
。 I also checked where condition with the old table(tblDoXang): 我还检查了旧表(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)));
It returned 7432 rows too. 它也返回了7432行。 However, when I test with a specific value, I received two different results(I expected they were the same).
但是,当我使用特定值进行测试时,我收到了两个不同的结果(我希望它们是相同的)。 This query returned two records(I select from the new table):
该查询返回了两条记录(我从新表中选择):
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'
while this threw 5 records(with the same condition)(I select from the old table with the condition I used to insert in the first query above): 而这抛出了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)));
I confused of this result and I didn't know exactly where was I wrong, so can someone give me some suggestion? 我对这个结果感到困惑,不知道自己到底在哪里错,所以有人可以给我一些建议吗? Thanks in advance.
提前致谢。
I have found my mistake. 我发现了我的错误。 In this query:
在此查询中:
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)));
I didn't specify time, accountID and deviceID. 我没有指定时间,accountID和deviceID。 It was different 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
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)));
Where I had specific where condition. 我在哪里有具体的地方条件。 So the query:
所以查询:
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'
correspond to this: 对应于此:
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;
That is the reason why I got the different result from two query above. 这就是为什么我从上面的两个查询中得到不同结果的原因。
Your idea was not good. 你的主意不好。 Please imagine that you have ten accounts in total.
请想象您总共有十个帐户。 With this query:
使用此查询:
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)));
you will only receive the max value(of nhienLieu
) in each one hour of ten accounts not of one account(which you expected). 您只会在十个帐户的每个小时中收到一小时的最大值(
nhienLieu
),而不是一个帐户的预期值。 So it is the reason why the result was difference between two cases: when you specified specifically accountID,deviceID and thoiGian and when you selected all in your table. 因此,这就是两种情况下结果不同的原因:当您专门指定accountID,deviceID和thoiGian时,以及在表中选择全部时。 I think that this query will satisfy you goal:
我认为此查询将满足您的目标:
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);
Pls try it and show me any problems if you get. 请尝试一下,如果有的话告诉我任何问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.