0

I am trying to insert data from csv file to mysql database. Data is inserted to DB. But not in the way I expected. In my case there are several text files which includes employee id, date, in time and out time. Also, I want to validate this if there is one record for the same employee number with same date it should not allow to insert the any data.

My Code

$fdate = array_filter(explode(" ", $y));

$csv->emp_id = $epf;
$a_date = date("Y-m-d", strtotime($fdate[9]));
$csv->date= $a_date;

$csv->save();

$check = "SELECT * FROM daily_attendances WHERE emp_id = $epf AND date = '$a_date'";

$sql = DB::select(DB::raw($check));
$count = count($sql);

if($count > 0){
  if ($fdate[11] == "AM") {
        $update_query = "UPDATE daily_attendances SET in_time = '$fdate[10]'  WHERE 
       emp_id = $epf AND date = '$a_date'";
       $sql = DB::select(DB::raw($update_query));

                            } elseif ($fdate[11] == "PM") {
                                $o_time = date("H:i:s", strtotime($fdate[10] . $fdate[11]));
                                $update_query = "UPDATE daily_attendances SET out_time = '$o_time'  WHERE emp_id = $epf AND date = '$a_date'";
                                $sql = DB::select(DB::raw($update_query));


                            }

current output

enter image description here

expected output enter image description here

6
  • Removal of duplicates should probably be done in the database, after you have loaded the CSV data. Commented Jul 10, 2017 at 4:25
  • I don't understand your expected output. Whence is the timestamp 18:18:21? It appears in neither of the two original rows. Commented Jul 10, 2017 at 4:26
  • Please add your csv data structure in file. Commented Jul 10, 2017 at 4:29
  • @TimBiegeleisen i have edited the question Commented Jul 10, 2017 at 4:31
  • What value gets inserted for the id in this case? Commented Jul 10, 2017 at 4:37

2 Answers 2

1

I recommend loading the CSV data into a temporary table first using MySQL's LOAD DATA. Once you have done that, you can insert into your target daily_attendances table using a query along the lines of the following:

INSERT INTO daily_attendances (emp_id, date, in_time, out_time)
SELECT emp_id, date, MAX(in_time), MAX(out_time)
FROM temp
GROUP BY emp_id, date

This answer assumes that the only sort of duplication or missing data is what you showed us in the question. In general, bringing in lots of new data from CSV will be fastest with LOAD DATA. Also, the kind of logic you require to scrub your data should be handled in the database.

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

Comments

0

It's better to check by using SQL query.

INSERT INTO TABLE_2 (id, name) SELECT t1.id,t1.name
FROM TABLE_1 t1
WHERE NOT EXISTS(SELECT id FROM TABLE_2 t2 WHERE t2.id = t1.id)

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.