Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help on insert comma separated data in mysql database using PHP

Demo data which already have in the database table:

INSERT INTO `csvtbl` (`ID`, `SKU`, `Product_Name`, `Model`, `Make`, `Year_From`, `Year_To`) VALUES
(1, 'C2AZ-3B584-AR', 'Power Steering Pump Seal Kit (Eaton Pump)', 'Galaxie', 'Ford', '1960', '1965'),
(2, 'C2AZ-3B584-AR', 'Power Steering Pump Seal Kit (Eaton Pump)', 'Thunderbird ', 'Fordtrest', '1960', '1965');

I have using below code and inserted years with comma(,) separated in table:

INSERT INTO `diff_yearstbl` (`ID`, `SKU`, `Product_Name`, `Model`, `Make`, `Year`) VALUES
(1, 'C2AZ-3B584-AR', 'Power Steering Pump Seal Kit (Eaton Pump)', 'Galaxie', 'Ford', '1960,1961,1962,1963,1964,1965'),
(2, 'C2AZ-3B584-AR', 'Power Steering Pump Seal Kit (Eaton Pump)', 'Thunderbird ', 'Fordtrest', '1960,1961,1962,1963,1964,1965');

Years data are working good, but I want to insert data Make and Model like same as Years, But this time just insert the data with comma (,) separated in one row like below example using SKU field. So need to merge the above two record in one row like below record.

INSERT INTO `diff_yearstbl` (`ID`, `SKU`, `Product_Name`, `Model`, `Make`, `Year`) VALUES
(1, 'C2AZ-3B584-AR', 'Power Steering Pump Seal Kit (Eaton Pump)', 'Galaxie, Thunderbird', 'Ford, Fordtrest' '1960,1961,1962,1963,1964,1965');

Below are the code what I have done:

    $query = "select Year_TO - Year_From as diff_years, ID, SKU,Product_Name,Model,Make,Year_From,Year_To from csvtbl";
$result = mysql_query($query, $connect );

//$result = $db->query($select_test)->fetchAll();

if (count($result) > 0) 
{

    while($QryRow = mysql_fetch_assoc($result)) 
    {
        $diff_years = $QryRow['diff_years'];
        $Year_From = $QryRow['Year_From'];
        $SKU = $QryRow['SKU'];
        $Product_Name = $QryRow['Product_Name'];
        $Model = $QryRow['Model'];
        $Make = $QryRow['Make'];

        $years= array();
        for ($x = $QryRow['Year_From']; $x <= $QryRow['Year_To']; $x++) 
        {           
            $years[] = $x;    
        }

        $query_insert = "INSERT INTO diff_yearstbl(SKU,Product_Name,Model,Make,Year) VALUES('".$SKU."','".$Product_Name."','".$Model."','".$Make."','".implode('|',$years)."')";
        $s_insert = mysql_query($query_insert, $connect ); 
    }
} 
else 
{
    echo "<p>Nothing matched your query.</p>";
}

    ?>

Please help about the same.

like image 446
Nishant Patel Avatar asked Feb 19 '16 05:02

Nishant Patel


People also ask

How do I display comma separated values in MySQL?

We can show the result as a comma separated list with the help of the 'concat()' function with the parameter ','. Let us see an example now. Firstly, we will create a table. The CREATE command is used to create a table.

Can we store comma separated values in MySQL?

You never store comma separated arrays in a database - each entry in the comma separated array needs to be stored in its own row in a table in the database. This table would have a many to one relationship to the original table.


1 Answers

In a single SQL statement, coping with up to 100 years:-

INSERT INTO diff_yearstbl(SKU, Product_Name, Model, Make, Year) 
SELECT   SKU,
        Product_Name,
        Model,
        Make,
        GROUP_CONCAT(Year_From + tens.acnt * 10 + units.acnt) AS ayear
FROM csvtbl
CROSS JOIN (SELECT 0 AS acnt UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) tens
CROSS JOIN (SELECT 0 AS acnt UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) units
WHERE (Year_From + tens.acnt * 10 + units.acnt AS ayear) <= Year_TO
GROUP BY SKU,
        Product_Name,
        Model,
        Make
like image 157
Kickstart Avatar answered Oct 28 '22 06:10

Kickstart