簡體   English   中英

使用where條件將表中的數據插入MySQL中的另一個表

[英]Insert data from a table to another one in MySQL with 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)));

它向我顯示了以下消息: 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)));

我對這個結果感到困惑,不知道自己到底在哪里錯,所以有人可以給我一些建議嗎? 提前致謝。

我發現了我的錯誤。 在此查詢中:

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。 它不同於:

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;

這就是為什么我從上面的兩個查詢中得到不同結果的原因。

你的主意不好。 請想象您總共有十個帳戶。 使用此查詢:

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時,以及在表中選擇全部時。 我認為此查詢將滿足您的目標:

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);

請嘗試一下,如果有的話告訴我任何問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM