0

I know there is plenty on here about duplicate rows and columns, but I have a single cell (intersection of row and column) that has multiple values that are comma delimited.

This would be somewhat trivial in some scripting language, but I want to keep in MySQL.

I have table checks and the last columns with there are two val1 values and only one should be returned.

+--------+----------+-------------+----------+----------------------------+
| id     | date     | doc         | amount   |  column_with_multiple_vals |
+--------+----------+-------------+----------+----------------------------+
| 346862 | 3/7/2017 | WIRE 111111 | 24651.33 | val1, val2, val3, val1     |
+--------+----------+-------------+----------+----------------------------+

Looking around I think this is how it might happen.

  1. Load Data Local Infile
  2. SET @myArrayOfValues = column_with_multiple_vals
  3. I am a little uncertain of how to go about deduplicating the values in this simulated MySQL array.

I found this ariticle and seemed like it was on the right track of how of what I am looking for.

How can I simulate an array variable in MySQL?

SET @myArrayOfValue = '2,5,2,23,6,';

WHILE (LOCATE(',', @myArrayOfValue) > 0)
DO
    SET @value = ELT(1, @myArrayOfValue);
    SET @myArrayOfValue= SUBSTRING(@myArrayOfValue, LOCATE(',',@myArrayOfValue) + 1);

    INSERT INTO `EXEMPLE` VALUES(@value, 'hello');
END WHILE;
Community
  • 1
  • 1
nzaleski
  • 433
  • 5
  • 14
  • 1
    Karwin, Bill "SQL Antipatterns: Avoiding the Pitfalls of Database Programming", Chapter 2 http://www.amazon.com/SQL-Antipatterns-Programming-Pragmatic-Programmers/dp/1934356557 – spencer7593 Mar 27 '17 at 12:55
  • 1
    Is it possible for you to put the values of column column_with_multiple_vals values into another table with a foreign key reference to id? – michaeak Mar 27 '17 at 12:58
  • This is probably an anti-pattern however this is how the data is coming over. This is just for analytical purpose. The transaction database/ERP is NetSuite and data structure is not designed in this way. I can probably re-work and get some other searches, but was hoping this method was possible. – nzaleski Mar 27 '17 at 13:15

0 Answers0