我有一个看起来像这样的.csv文件......
Name AC-No. Time State Exception Operation
Johnny Starks Depp 1220 4/12/2013 12:45:18 AM Check In OK
Johnny Starks Depp 1220 4/12/2013 5:46:58 AM Out Out
Johnny Starks Depp 1220 4/12/2013 6:22:41 AM Out Back Out
Johnny Starks Depp 1220 4/12/2013 10:42:17 AM Check Out Repeat
Johnny Starks Depp 1220 4/12/2013 10:42:19 AM Check Out OK
我已经可以将其上传到我的数据库了。我的问题是,它在表格中显示的结果。 在mysql数据库中,我有一个带有列的表( Name,ACNo。,Date,CheckIn,Breakout,Breakin,CheckOut )。我想要发生的事情是我做不到的..具有相同 DATE 的.csv文件中的记录/(显示在TIME列中)将在同一行中使用TABLE中的Check In,BreakOut,BreakIn和CheckOut,不会产生多条记录。 喜欢这个..
Name |ACNo | Date | CheckIn | Breakout | Breakin | Checkout
| | | | | |
Johnny Starks Depp|1220 | 4/12/2013 | 12:45:18 AM | 5:46:58 AM | 6:22:41 AM | 10:42:19 AM
现在..我已经做了我想要在表中发生的上述例子但是...当我检查表时,是的它每行有不同的日期但是时间(CheckIn,Breakout,Breakin,Checkout) ) 错了。错了,因为例如DATE 4/12/2013中的时间记录在DATE 4/13/2013中。我使用的代码有什么问题?或者有什么方法可以做到这一点吗?这些是我的代码:
<?php
$conn = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("dtrlogs",$conn);
$datehere ="";
if(isset($_POST['Submit']))
{
$file = $_FILES['file']['tmp_name'];
if($file == "")
{
?>
<script type="text/javascript">
alert('No File Selected!');
</script>
<?php
}
else
{
$handle = fopen($file,"r");
while(($fileop = fgetcsv($handle,1000,";")) !==false)
{
$Name = $fileop[0];
$ACNo = $fileop[1];
$Time = $fileop[2];
$State = $fileop[3];
$NewState = $fileop[4];
$Exception = $fileop[5];
$Operation = $fileop[6];
//i separated time/date in the Column TIME in the .csv file
$date = date('m/d/Y', strtotime($Time));
$hours = date('H:i:s A', strtotime($Time));
//this is what i used to prevent multiple dates
if($datehere != $date) {
$datehere = $date;
}
else{
$date = "";
}
//this is to insert the name and acno to the other table.
//(this is for the lists of employee)
$sql=mysql_query("SELECT * FROM employee Where ACNo = '$ACNo'");
$row=mysql_fetch_array($sql);
if($row == 0) {
$sql1 = mysql_query("INSERT INTO employee(ACNo, Name) VALUES('$ACNo','$Name')");
}
//Inserts record if Date is not yet recorded and if already has just updates
//the row's column (Checkin, breakout, breakin and Checkout)
//with its correct time according to the date
$query = mysql_query("SELECT * FROM dtrs Where ACNo = '$ACNo' AND Date = '$date'");
$rows = mysql_fetch_array($query);
if($rows == 0) {
$sql2 = mysql_query("INSERT INTO dtrs(Name, ACNo, Date, CheckIn, BreakOut,
BreakIn,CheckOut)
VALUES('$Name','$ACNo','$date','$CheckIn','$Breakout','$Breakin','$Checkout')");
}
else{
$sql2 = mysql_query("Update dtrs Set CheckIn = '$CheckIn', BreakOut =
'$Breakout', BreakIn = '$Breakin', CheckOut = '$Checkout'");
}
//this is my conditions to identify whether the TIME/HOUR is
//Stated as CheckIn, Breakout, Breakin, or Checkout
if($NewState == "Check In" || $State == 'Check In' && $Exception == "OK") {
if($Exception == "Invalid" || $Exception == "Repeat")
{}
else {
$CheckIn = $hours;
}
}
if($State == 'Out' && $Exception == "Out") {
if($Exception == "Invalid" || $Exception == "Repeat")
{}
else {
$Breakout = $hours;
}
}
if($State == 'Out Back' && $Exception == "Out") {
if($Exception == "Invalid" || $Exception == "Repeat")
{}
else {
$Breakin = $hours;
}
}
if($NewState == "Check Out" || $State == "Check Out" && $Exception == "OK") {
if($NewState == "Check In")
{}
else {
$Checkout = $hours;
}
}
}
if($sql2)
{
if($sql2)
{
?>
<script type="text/javascript">
alert('File Upload Successful!');
</script>
<?php
}
}
}
}
?>
答案 0 :(得分:0)
我建议可能加载到一个临时表,并从中提取用于填充真实表的细节。
例如,如果您将CSV文件加载到名为LoadTable的表(不是临时表,因为您只能在一段SQL中使用临时表),您可以提取不同的名称和帐号,然后将其加入subselects以获取每个相关事件的最大事件时间。
这样的事情: -
SELECT DerivMain.Name,
DerivMain.ACNo,
DerivMain.EventDate,
DATE_FORMAT(DerivCheckIn.EventDateTime, '%l:%i:%s %p'),
DATE_FORMAT(DerivBreakout.EventDateTime, '%l:%i:%s %p'),
DATE_FORMAT(DerivBreakin.EventDateTime, '%l:%i:%s %p'),
DATE_FORMAT(DerivCheckout.EventDateTime, '%l:%i:%s %p')
FROM (SELECT DISTINCT Name, ACNo, DATE(STR_TO_DATE(Date,'%c/%e/%Y %l:%i:%s %p')) As EventDate FROM LoadTable) DerivMain
LEFT OUTER JOIN (SELECT Name, ACNo, MAX(STR_TO_DATE(Date,'%c/%e/%Y %l:%i:%s %p') AS EventDateTime) FROM LoadTable WHERE State = 'Check In' GROUP BY Name, ACNo) DerivCheckIn
ON DerivMain.Name = DerivCheckIn Name AND DerivMain.ACNo = DerivCheckIn.ACNo
LEFT OUTER JOIN (SELECT Name, ACNo, MAX(STR_TO_DATE(Date,'%c/%e/%Y %l:%i:%s %p') AS EventDateTime) FROM LoadTable WHERE State = 'Out' GROUP BY Name, ACNo) DerivBreakout
ON DerivMain.Name = DerivBreakout Name AND DerivMain.ACNo = DerivBreakout.ACNo
LEFT OUTER JOIN (SELECT Name, ACNo, MAX(STR_TO_DATE(Date,'%c/%e/%Y %l:%i:%s %p') AS EventDateTime) FROM LoadTable WHERE State = 'Out Back' GROUP BY Name, ACNo) DerivBreakin
ON DerivMain.Name = DerivBreakin Name AND DerivMain.ACNo = DerivBreakin.ACNo
LEFT OUTER JOIN (SELECT Name, ACNo, MAX(STR_TO_DATE(Date,'%c/%e/%Y %l:%i:%s %p') AS EventDateTime) FROM LoadTable WHERE State = 'Check Out' GROUP BY Name, ACNo) DerivCheckout
ON DerivMain.Name = DerivCheckout Name AND DerivMain.ACNo = DerivCheckout.ACNo