Skip to content
Advertisement

parse string containing comma in csv using mysql? (or maybe pandas?)

(I’m using mysql v5.7)

I have a csv file that has something like

mytable1

note that ‘aaa,bbb,ccc’ is one string and ‘8,3,5’ is one string.. I know it’s not a good practice putting data like this but this is what I have given from other department… so my first task was to extract number matched with ccc. like this:

mytable2

for the name3, there were two ‘ccc’ but it is ok to pick the first one.

I could get the right result by doing

so basically, I found the index of the ccc then use that index to find the matched id. (btw max number of items in sys column is 6 like ‘aaa, bbb, ccc, ccc, ccc, ccc’)

now I am wondering if there’s any way to find the ids that’s from ccc but not included in the mytable2. like this.

any help would be appreciated!!

Advertisement

Answer

The id matched the first specified sys may be found by simple

For to extract all another values use:

You may remove name from the output if you do not need in it.

Increase the numbers count in numbers subquery accordingly if the sys/id value may contain more than 4 separate values in CSV.

fiddle

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement