我对SQL方面还很陌生。我一直在尝试向您介绍条件,但是让它确定查询是否等于1个结果或多个结果。我的物品在下面。
我可能做错了所有,但以为我会问。
SELECT employee_name
, transaction_id
, department
, transaction_date
, transaction_day
FROM employee_transactions
WHERE transaction_date
BETWEEN '2018-08-20 07:14:35.000'
AND '2019-02-05 08:29:56.000'
AND CASE WHEN '$variable' = 'All'
THEN inc_day IN (
SELECT DISTINCT transaction_day
FROM employee_transactions
) ELSE inc_day in ('$variable')
END
在列交易日中,我的意思是星期一,星期二,星期三等条目存在,但都不在那里。因此,在表单上,如果他们选择了全天所有的数据,而不只是一个,但是如果他们选择了周一的数据,则只显示monda
答案 0 :(得分:1)
您应该像下面这样编写查询。
SELECT employee_name,
transaction_id,
department,
transaction_date,
transaction_day
FROM employee_transactions
WHERE transaction_date
BETWEEN '2018-08-20 07:14:35.000'
AND '2019-02-05 08:29:56.000'
AND ( '$variable' = 'All' OR inc_day = '$variable')
当$ variable ='ALL'时,上述查询将返回所有记录,否则仅匹配记录。
假设“ $ variable”将在执行查询之前被替换。
答案 1 :(得分:0)
您可以尝试这样
if($variable == "All")
{
$query = "SELECT employee_name, transaction_id , department , transaction_date ,
transaction_day
WHERE transaction_date BETWEEN '2018-08-20 07:14:35.000' AND '2019-02-05 08:29:56.000'
GROUP BY transaction_date"
}
else
{
$query = "SELECT employee_name, transaction_id , department , transaction_date , transaction_day
FROM employee_transactions
WHERE transaction_date BETWEEN '2018-08-20 07:14:35.000' AND '2019-02-05 08:29:56.000'
AND inc_day in ($variable)"
}