phpmysqlload-data-infile

How to LOAD DATA LOCAL INFILE using @column with if statement


I need to set color based on color code using @set on Load Data Local INFILE

 LOAD DATA LOCAL INFILE "'.$file.'"
        INTO TABLE '.$table.'
        FIELDS TERMINATED by \',\'
        ENCLOSED BY \'"\'
        ESCAPED BY \'"\'
        LINES TERMINATED BY \'\n\'
        (Brand, @Color, @Year)
        SET
        Color = if (@Color = "1b"){ Color = "Blue" } else { Color = "Green" },
        Year = "2022"


Solution

  • you have to use the mysql style of the if clause

    LOAD DATA LOCAL INFILE "'.$file.'"
            INTO TABLE '.$table.'
            FIELDS TERMINATED by \',\'
            ENCLOSED BY \'"\'
            ESCAPED BY \'"\'
            LINES TERMINATED BY \'\n\'
            (Brand, @Color, @Year)
            SET
            Color = if (@Color = '1b',  'Blue' ,  'Green' ),
            Year = "2022"
    

    For more option

    LOAD DATA LOCAL INFILE "'.$file.'"
            INTO TABLE '.$table.'
            FIELDS TERMINATED by \',\'
            ENCLOSED BY \'"\'
            ESCAPED BY \'"\'
            LINES TERMINATED BY \'\n\'
            (Brand, @Color, @Year)
            SET
            Color = CASE @Color 
            WHEN '1b' THEN  'Blue' 
            WHEN '2b' THEN 'brown'
            ELSE  'Green' 
            END,
            Year = "2022"