我有此查询可以在销售日期字段中合并日期。
一旦此人有超过3个购买日期并且我们运行查询,他们根本不会显示。如果他们有两个或三个不是问题,只要有四个日期来组合它就会一起跳过这条线。
<?php
set_time_limit(0);
$server = 'localhost';
$login = 'xxxx';
$password = 'xxxx';
$db = 'opg';
$table = 'OPG';
$filename = '/home/.sites/143/site2/web/csv/OPG.csv';
mysql_connect($server, $login, $password);
mysql_select_db($db);
$fp = fopen($filename, "w");
$query = 'SET GLOBAL group_concat_max_len=15000';
mysql_query($query);
$res = mysql_query("SELECT t1.employee_number, t1.first_name, t1.last_name
, GROUP_CONCAT( DATE_FORMAT( t1.dates_of_purchase, '%m-%d-%y' ) SEPARATOR ' & ' ) AS Purchase_Date
, t1.Dept, t1.Location
FROM OPG AS t1
LEFT JOIN OPG AS t2 ON ( t1.employee_number = t2.employee_number
AND t1.last_name = t2.last_name
AND t1.QTY != t2.QTY )
WHERE t1.dates_of_purchase >= '2011-01-01'
AND t1.QTY >=1
AND t2.employee_number IS NULL
GROUP BY t1.employee_number, t1.first_name, t1.last_name
ORDER BY t1.employee_number
");
mysql_data_seek($res, 0);
while($row = mysql_fetch_assoc($res)) {
$line = "";
$comma = "opg,";
foreach($row as $value) {
$line .= $comma . '' . str_replace('', '', $value) . '';
$comma = ",";
}
$line .= "\n";
fputs($fp, $line);
}
fclose($fp);`
这是代码,它在大多数情况下都有效。 为什么在超过3个购买日期之前不会返回任何行?
以下是数据:
Account employee_number first_name last_name dates_of_purchase Dept Location QTY Vendor id ONTH30 652526 G HARRIPERSAD 2011-02-16 PWU NUCLEAR SUPPLY CHAIN 1 MSS 1836 ONTH30 652561 AJJ FELS 2011-09-15 PWU NOSS 1 MSS 4319 ONTH30 652561 AJJ FELS 2011-06-23 PWU NOSS 1 MSS 8898 ONTH30 652561 AJJ FELS 2011-08-18 PWU NOSS 1 MSS 8938 ONTH30 652526 G HARRIPERSAD 2011-11-16 PWU PICKERING 1 MSS 7231 ONTH30 652561 AJJ FELS 2011-12-08 PWU NOSS -1 MSS 8141 ONTH30 652561 AJJ FELS 2011-10-13 PWU NOSS 1 MSS 9055
输出中缺少的数据是员工编号652561。 它不会输出到CSV文件,因为某种原因它会一起跳过它。
输出应如下所示:
opg, 652561, AJJ, FELS, 10-13-2011 & 12-08-2011 & 08-18-2011 & 06-23-2011 & 09-15-2011 , ESR, PICKERING A