在数据表服务器端脚本中使用LEFT JOIN

时间:2010-08-23 02:17:08

标签: jquery mysql datatable left-join

亲爱的,我将从数据库中获取一些数据。 并加入两个表。代码如:

SELECT DATE(A.Inspection_datetime) AS Date, 
  A.Model, 
  COUNT(A.Serial_number) AS Qty, 
  B.Name 
FROM inspection_report AS A 
LEFT JOIN Employee AS B ON A.NIK = B.NIK 
 GROUP BY A.Model, A.Serial_number

我希望使用jQuery dataTable显示这些数据。我试图在dataTable的服务器端脚本进行修改。我在这部分已经改变了:

$sWhere = " WHERE Inspection_datetime LIKE '%".mysql_real_escape_string( $_POST['sSearch'] )."%' 
  OR Model LIKE '%".mysql_real_escape_string( $_POST['sSearch'] )."%' 
  OR Serial_number LIKE '%".mysql_real_escape_string( $_POST['sSearch'] )."%' 
  OR NIK LIKE '%".mysql_real_escape_string( $_POST['sSearch'] )."%' ";

$sQuery = "SELECT id, 
  DATE(A.Inspection_datetime) AS Date, 
  A.Model, 
  COUNT(A.Serial_number) AS Qty, 
  B.Name 
  FROM inspection_report AS A 
  LEFT JOIN Employee AS B ON A.NIK = B.NIK .$sWhere.$sOrder.$sLimit";

$sOutput .= "[";
  $sOutput .= '"'.addslashes($aRow['id']).'",';
  $sOutput .= '"'.addslashes($aRow['Date']).'",';
  $sOutput .= '"'.addslashes($aRow['Model']).'",';
  $sOutput .= '"'.addslashes($aRow['Qty']).'",';
  $sOutput .= '"'.addslashes($aRow['Name']).'"';
  $sOutput .= "],";

我希望结果像我的mysql代码一样,但这就像:

{"sEcho":1,"iTotalRecords":2,"iTotalDisplayRecords":2, "aaData":[["42","","bar","","Steve"]]}

1 个答案:

答案 0 :(得分:0)

我做了什么?我很粗心。这些都要被替换。

$sOutput .= "[";
                $sOutput .= '"'.addslashes($aRow['id']).'",';
                $sOutput .= '"'.addslashes($aRow['Date').'",';
                $sOutput .= '"'.addslashes($aRow['Model']).'",';
                $sOutput .= '"'.addslashes($aRow['Qty']).'",';
                $sOutput .= '"'.addslashes($aRow['Name']).'"';
                $sOutput .= "],";