我每天都收到一个带有电话号码的csv文件。我已经构建了一个从csv中获取数据的数组。我需要做的是遍历数组并在mysql数据库中搜索匹配项。你的一位大师可以如此善良地指出我正确的方向吗?
答案 0 :(得分:2)
如果您能够,请使用LOAD DATA
将csv导入临时表,并根据您的电话号码编写与该表匹配的查询。
答案 1 :(得分:2)
我建议创建临时表,然后将数字加载到此表中,然后只需选择&加入你的桌子。您可以在程序中插入手机(多个行在一个INSERT
中),或者如果您有权访问服务器shell,则可以执行以下脚本:
-- you can use TEMPORARY table in one transaction or you can just
-- make sure that the table exists and flush it before import
CREATE TABLE IF NOT EXISTS phonenumbers (
number varchar(10) primary key
);
TRUNCATE TABLE phonenumbers;
LOAD DATA LOCAL INFILE 'numbers.csv' INTO TABLE phonenumbers;
-- here in script or call it from your program to work with data
-- also you can add INTO OUTFILE 'output.csv' to export filtered data
-- into outfile
SELECT addressbook.* FROM addressbook LEFT JOIN phonenumbers on (addressbook.phone = phonenumbers.number);