我写了代码,用PHP从mysql数据库下载excel文件。但是我想从数据库下载定制的excel文件的地方。就像,在我的数据库中有name,inst1date,inst2date,inst3date....。我希望在我的Excel中将有name和date字段,其中date将在两个我要输入的日期之间。它选择匹配的名称以及相应的inst1date或inst2date或inst3date....。
sqlite3 yourdbname <<EOF
.separator ,
select * from yourtablename;
.quit
EOF
答案 0 :(得分:0)
调整您的SQL
$query= '
SELECT
name, DATE_FORMAT(inst1date, "%M %d %Y") AS inst1date ...
FROM installment
WHERE inst1date BETWEEN `'.$search_text.'` and `'.$search_text2.'`
LIMIT 100
';
*
,因为它将从表中获取可能不需要的所有内容。而是使用field1,field2
$d = date("format", time);
-更新
选择
*
从分期付款
哪里
(
在'.$search_text.'
和'.$search_text2.'
之间安装
) 要么
(
'.$search_text.'
和'.$search_text2.'
之间的inst2date
) 要么
(
'.$search_text.'
和'.$search_text2.'
之间的inst3date
)
限制100
将返回inst1date,inst2date或inst3date中的任何一个在搜索词之间的行。您需要在PHP中处理结果,因为您的表对于这种类型的查询而言设计不佳。
while($rec =$stmt->FETCH(PDO::FETCH_ASSOC)){
$rowData = '';
foreach($rec as $key => $value)
{
if(
( $key == 0 ) || // Will pass for the name param as it is the first in your row
(
$rec["inst1date"]) >= strtotime($search_text) && strtotime($rec["inst1date"]) <= strtotime($search_text)
)||
// Repeat for inst2date and inst3date
){
$value = '"' . $value . '"' . "\t";
$rowData .= $value;
}
}
$setData .= trim($rowData)."\n";
}
//这是一个php解决方案。这很混乱,因为SQL表有点混乱
更改sql查询会产生更好的结果
SELECT
name,
#You could also add a format function too
CASE
WHEN inst1date BETWEEN `'.$search_text.'` and `'.$search_text2.'` THEN inst1date
WHEN inst2date BETWEEN `'.$search_text.'` and `'.$search_text2.'` THEN inst2date
ELSE inst3date
END AS date
FROM installment
WHERE
(
inst1date BETWEEN `'.$search_text.'` and `'.$search_text2.'`
) OR
(
inst2date BETWEEN `'.$search_text.'` and `'.$search_text2.'`
) OR
(
inst3date BETWEEN `'.$search_text.'` and `'.$search_text2.'`
)
LIMIT 100
上面的查询将仅返回两个字段(名称字段和日期字段)
-第二次更新
我认为,如果您像这样重构表,那么查询起来会容易100%。示例:
id | name | date
________|____________|_______________
1 | Arjun | 2019-04-03 23:00:01
________|____________|________________
2 | Sachin | 2019-04-05 22:00:01
________|____________|________________
3 | Sachin | 2019-06-05 21:00:01
只需将要跟踪的每个实例插入表中即可。使用user_id和联接会更好(用users表中的user_id代替名称)
$query= '
SELECT
CONCAT(u.first_name, " ", u.last_name) AS name,
DATE_FORMAT(date, "%Y-%m-%d") AS date
FROM installment AS i
JOIN users AS u ON u.id = i.user_id
WHERE date BETWEEN `'.$search_text.'` and `'.$search_text2.'`
#OPTIONALLY ADD GROUP BY TO LIMIT RESULTS
LIMIT 100
';
使用上述内容将利用数据库的关系性质。垂直而不是水平重构该稳定对象将允许您执行inst1date,inst2date,inst3date,inst4date等。对于每个条目,您实际上都在添加另一个日期字段。显然,您可以看到这是一种更好的组织化技术。同样,您可以创建更深入的查询,因为可以包含更多数据,而不必不断添加字段。