基于下拉选择的SQL查询

时间:2016-10-07 19:24:20

标签: php mysql database date

我想根据下拉选择值编写SQL查询。

例如,如果管理员今天选择,那么SQL查询应该只获得今天的记录等等。

HTML

<select name="report_date">
    <option value="Today">Today</option>
    <option value="Yesterday">Yesterday</option>
    <option value="Last7Days">Last 7 Days</option>
    <option value="This Week">This Week</option>
    <option value="LastWeek">Last Week</option>
    <option value="ThisMonth">This Month</option>
    <option value="LastMonth">Last Month</option>
    <option value="ThisYear">This Year</option>
    <option value="LastYear">Last Year</option>
    <option value="AllTime">All Time</option>
</select>

<input type="text" name="product_name"/>

PHP

$fixedrange = $_GET['report_date'];
$pname= $_GET['product_name'];

if($fixedrange=="Today"){
    $today = date('Y-m-d');
    $fixeddate = 'OR o.`custom_date`='. $today;
}

SQL查询

SELECT *
FROM order_items AS oi
INNER JOIN orders o ON o.id=oi.`order_id`
WHERE oi.`product_name` LIKE '%$pname%' $fixeddate
  AND o.status = '1'
ORDER BY o.custom_date DESC

2 个答案:

答案 0 :(得分:0)

你应该使用concat

Integer.MAX_VALUE

或者不使用php日期,您可以使用$sql = " SELECT * FROM order_items AS oi INNER JOIN orders o ON o.id=oi.`order_id` WHERE oi.`product_name` LIKE concat('%', $pname,'%') $fixeddate AND o.status = '1' ORDER BY o.custom_date DESC ; "; custom_date date(o.

) = curdate()

答案 1 :(得分:0)

 SELECT OrderId,DATE_ADD(date(OrderDate),INTERVAL + 30 DAY) AS OrderPayDate
 FROM Orders

 use +(plus) or - (minus) for adding future date or subtract for past date using DATE_ADD sql function