Can we import a file with unknown columns all into one column?
Yes you can do it a few steps:
First Create a temporary staging table (lets call it import
) with one column that will hold lines as rows of the source data file
CREATE TABLE `import` (
`line` varchar(512) DEFAULT NULL
);
and load data from the file into the table
LOAD DATA INFILE '/path/to/your/file.txt'
INTO TABLE import
FIELDS TERMINATED BY '\n'
LINES TERMINATED BY '\n'
Second Assuming that you have a target table (lets call it words
) that looks something like this
CREATE TABLE `words` (
`word` varchar(64) DEFAULT NULL
);
we can split each line into words and insert them into words
table with a query
INSERT INTO words (word)
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(line, ' ', n.n), ' ', -1) word
FROM import CROSS JOIN
(
SELECT a.N + b.N * 10 + 1 n
FROM
(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a
,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) b
ORDER BY n
) n
WHERE n.n <= 1 + (LENGTH(line) - LENGTH(REPLACE(line, ' ', '')))
ORDER BY n.n
NOTE:
- assumption is that your words separated exactly with one space
- this query will split up to 100 words. If you need more or less you can adjust a limit by editing the inner subquery or if do import regularly than consider to use persisted tally(numbers) table instead.
Third Drop or truncate (if you will use it again to import more files) the staging table
DROP TABLE import;
Here is SQLFiddle demo that shows step two.