Php SQL联接表日期搜索
我正在尝试为SQL查询添加按日期搜索表单。没有WHERE的原始查询运行良好,因此我知道我没有正确地将日期传递给查询。当我搜索时,什么也没有返回。以下是我所拥有的:Php SQL联接表日期搜索,php,mysql,Php,Mysql,我正在尝试为SQL查询添加按日期搜索表单。没有WHERE的原始查询运行良好,因此我知道我没有正确地将日期传递给查询。当我搜索时,什么也没有返回。以下是我所拥有的: $start = $_POST['start']; $end = $_POST['end']; $search_start = date('Y-m-d', strtotime(str_replace('-', '/', $start))); $search_end = date('Y-m-d', strt
$start = $_POST['start'];
$end = $_POST['end'];
$search_start = date('Y-m-d', strtotime(str_replace('-', '/', $start)));
$search_end = date('Y-m-d', strtotime(str_replace('-', '/', $end)));
//exports 2016-04-01 2016-04-06
// Get Report
$query = "SELECT wp_ch_ifs_neworders.order_contact_id, wp_ch_ifs_neworders.order_date, wp_ch_ifs_neworders.order_contact, wp_ch_ifs_neworders.order_first_name, wp_ch_ifs_neworders.order_last_name, wp_ch_ifs_contacts.contact_email, wp_ch_ifs_contacts.contact_phone, wp_ch_ifs_neworders.order_address, wp_ch_ifs_neworders.order_city, wp_ch_ifs_neworders.order_state, wp_ch_ifs_neworders.order_zip
FROM wp_ch_ifs_contacts
INNER JOIN wp_ch_ifs_neworders
ON wp_ch_ifs_contacts.contact_id=wp_ch_ifs_neworders.order_contact_id
WHERE wp_ch_ifs_neworders.order_date BETWEEN %d AND %s, $search_start, $search_end
ORDER BY wp_ch_ifs_neworders.order_date DESC;";
表中的日期和搜索日期都是这样显示的
2016-04-01
,因此我觉得它应该可以正常工作在SQL查询中,日期需要用引号括起来。在查询中绝对没有理由使用%d
和%s
,它们是与sprintf
一起使用的格式运算符
$query = "SELECT wp_ch_ifs_neworders.order_contact_id, wp_ch_ifs_neworders.order_date, wp_ch_ifs_neworders.order_contact, wp_ch_ifs_neworders.order_first_name, wp_ch_ifs_neworders.order_last_name, wp_ch_ifs_contacts.contact_email, wp_ch_ifs_contacts.contact_phone, wp_ch_ifs_neworders.order_address, wp_ch_ifs_neworders.order_city, wp_ch_ifs_neworders.order_state, wp_ch_ifs_neworders.order_zip
FROM wp_ch_ifs_contacts
INNER JOIN wp_ch_ifs_neworders
ON wp_ch_ifs_contacts.contact_id=wp_ch_ifs_neworders.order_contact_id
WHERE wp_ch_ifs_neworders.order_date BETWEEN '$search_start' AND '$search_end'
ORDER BY wp_ch_ifs_neworders.order_date DESC;";
我就是这么想的。我是在修改公司过去的开发人员而不是SQL大师的代码。我试试这个