0

I have a CSV file containing user information:

'Arlington', '1,3,5,7,9'
'StackExchange', '2,3'

And I will need the above information imported like this:

"User" table:

id | name
1  | 'Arlington'
2  | 'StackExchange'

"User groups" table:

id | user_id | group_id
1  | 1       | 1
2  | 1       | 3
3  | 1       | 5
4  | 1       | 7
5  | 1       | 9
6  | 2       | 2
7  | 2       | 3

What's the easiest way to do this? I have imported the data with a temp column holding the CSV values:

id | name            | tmp_group_ids
1  | 'Arlington'     | '1,3,5,7,9'
2  | 'StackExchange' | '2,3'

I am thinking if I import it this way, I will know exactly what id gets assigned for the user (the id column in the users table is auto_increment), and so I can use that id as user_id for the "user groups" table.

But now how do I get values from tmp_group_ids into the "User groups" table?

Would appreciate any help! Thanks!

4 Answers 4

1

the easy way would be a php or perl script.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the MySQL SUBSTRING() function to split the string and insert the different values into the table. You can do this by writing a function or using a stored procedure.

1 Comment

If there's no characters in the CSV fields that are escaped, at least. Given that, it would be the most straightforward solution.
0

I had recently a similar problem, I used the function SUBSTRING_INDEX(str,delim,count), using "," as delimiter

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring-index

INSERT INTO tableUserGroup (userid, groupid)
SELECT 
    t1.id
    , substring_index(t1.tmp_group_ids,',',2)
    , substring_index(t1.tmp_group_ids,',',3)
FROM table1 t1

1 Comment

Found your similar question on SE: link So I guess I can call this N times if I know where will be N values at most in the CSV column, as something quick and dirty? INSERT INTO user_group (user_id, group_id) SELECT id, substring_index(tmp_group_ids,',',[N]) FROM users
0

First, insert the names into the User table - with id autonumber, this will work:

INSERT INTO User
  (name)
SELECT DISTINCT
    name
FROM TempTable 

Then:

--- Create a "numbers" table:
CREATE TABLE num
( i INT PRIMARY KEY
) ;

--- Populate it with numbers:
INSERT INTO num
  (i)
VALUES
  (1),(2),(3),(4),(5),(6),(7),(8),(9),(10);  

Then, you can use FIND_IN_SET() function which is handy for this situation (splitting comma-separated fields), like this:

INSERT INTO User_Groups
  (user_id, group_id)
SELECT
    u.id  AS user_id
  , num.i AS group_id
FROM User AS u
  JOIN TempTable AS t
    ON t.name = u.name
  JOIN num
    ON FIND_IN_SET(num.i, t.tmp_group_ids) > 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.