<?php
//用户名称
$userName= isset($_GET['userName']) ? trim($_GET['userName']) : NULL;
//起始时间
$dateStart= isset($_GET['dateStart']) ? strtotime(trim($_GET['dateStart'])) : NULL;
//终止时间
$dateEnd= !empty($_GET['dateEnd']) ? strtotime(trim($_GET['dateEnd']).' 23:59:59') : NULL;
if(empty($userName)){
die('请输入用户名');
}
if(!empty($userName)){
$where .= ' where `user_name`="'.$userName.'"';
}
if(!empty($dateStart)){
$where .= ' and `start_time`>'.$dateStart;
}
if(!empty($dateEnd)){
$where .= ' and `end_time`<'.$dateEnd;
}
?>对于上面的代码,我的困惑在于,用户名,起始时间和终止时间都可能有输入,但是不知道如果最好地建立索引。请问大家如何根据上面的PHP代码,为Mysql表中的 user_name,start_time,end_time建立组合索引呢?谢谢!
//用户名称
$userName= isset($_GET['userName']) ? trim($_GET['userName']) : NULL;
//起始时间
$dateStart= isset($_GET['dateStart']) ? strtotime(trim($_GET['dateStart'])) : NULL;
//终止时间
$dateEnd= !empty($_GET['dateEnd']) ? strtotime(trim($_GET['dateEnd']).' 23:59:59') : NULL;
if(empty($userName)){
die('请输入用户名');
}
if(!empty($userName)){
$where .= ' where `user_name`="'.$userName.'"';
}
if(!empty($dateStart)){
$where .= ' and `start_time`>'.$dateStart;
}
if(!empty($dateEnd)){
$where .= ' and `end_time`<'.$dateEnd;
}
?>对于上面的代码,我的困惑在于,用户名,起始时间和终止时间都可能有输入,但是不知道如果最好地建立索引。请问大家如何根据上面的PHP代码,为Mysql表中的 user_name,start_time,end_time建立组合索引呢?谢谢!
create index idx01 on xxxtable(user_name,start_time);
create index idx02 on xxxtable(user_name,end_time);
另外建议在MYSQL版中尽量直接贴你的SQL语句,不要再让别人来分析你的PHP代码。并不是每个MYSQL使用者都需要去懂PHP的。
问题说明越详细,回答也会越准确!参见如何提问。(提问的智慧)
但很肤浅,只是学校老师的作业,所以没涉及这么深,有些索引都没建。
create index idx02 on xxxtable(user_name,end_time);
if(!empty($userName)){
$where .= ' where `user_name`="'.$userName.'"';
}
如果为空,下面的会报错
所以在SQL语句的第一句加上 SQL + WHERE 1
$where.=" where 1=1 ";
if(!empty($userName)){
$where .= ' `user_name`="'.$userName.'"';
}
if(!empty($dateStart)){
$where .= ' and `start_time`>'.$dateStart;
}
if(!empty($dateEnd)){
$where .= ' and `end_time`<'.$dateEnd;
}
//user_name,start_time,end_time好上建索引
die('请输入用户名');
}
这一句已经确保了$userName 不为空。