嘗試文本文件加載到臨時表(相同目標表),然後從臨時表刪除重複,其餘複製到目標表。
示例(假設wbrdatatable_temp是從文本文件的所有數據的臨時表):
CREATE TABLE wbrdatatable(
id INT(11) NOT NULL AUTO_INCREMENT,
column1 VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (id)
);
INSERT INTO wbrdatatable VALUES
(1, '111'),
(2, '222'),
(3, '333'),
(4, '444'),
(5, '555');
CREATE TABLE wbrdatatable_temp(
id INT(11) NOT NULL AUTO_INCREMENT,
column1 VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (id)
);
INSERT INTO wbrdatatable_temp VALUES
(1, '111'),
(2, '222'),
(10, '100'), -- new record that should be added
(11, '200'); -- new record that should be added
-- Copy only new records!
INSERT INTO wbrdatatable
SELECT t1.* FROM wbrdatatable_temp t1
LEFT JOIN wbrdatatable t2
ON t1.id = t2.id AND t1.column1 = t2.column1
WHERE t2.id IS NULL;
-- Test result
SELECT * FROM wbrdatatable;
+----+---------+
| id | column1 |
+----+---------+
| 1 | 111 |
| 2 | 222 |
| 3 | 333 |
| 4 | 444 |
| 5 | 555 |
| 10 | 100 | -- only new record is added
| 11 | 200 | -- only new record is added
+----+---------+
你用什麼程序來加載文本文件到mysql – BizApps
在java中我寫的程序來執行上述查詢。 – sankar
我正在使用簡單的JDBC程序來加載執行此查詢字符串str =「將數據infile'F://Hello.txt'加載到表中由'',''選擇性封閉的newwbrtable字段可能由\」\「括起來的行由'\ r \ n'忽略1行(時間,頻道1,頻道2);「; Class.forName(driverclass).newInstance(); con = DriverManager.getConnection(jdbcURL,user,password); st = con.createStatement(); – sankar