我在下面有MySQL查询,它将自定义表的结果分配给我在WHERE子句中使用的变量。
SET @Customer_Group = (SELECT CustomerGroup FROM car_promisquestions);
SET @Department = (SELECT Department FROM car_promisquestions);
SET @Due_Date = (SELECT DueDate FROM car_promisquestions);
SET @PriorDate = (SELECT PriorDate FROM car_promisquestions);
SET @QDate = IF( @PriorDate = 'Y', CONCAT('saw_order.`Date` > ', '(@Due_Date - INTERVAL 5 DAY)'), CONCAT("saw_order.`Date` = ", @Due_Date));
SELECT
saw_order.Wo,
saw_orderitem.Item,
saw_order.Date,
saw_orderitem.Qty,
saw_orderitem.ModelDescription,
saw_orderitem.ColorDescription,
saw_order.`Status`,
saw_orderitem.Product,
saw_orderitemdep.Employee,
saw_orderitemdep.EmpLastName,
saw_orderitemdep.EmpFirstName,
saw_orderitemdep.Department,
saw_orderitem.ProductDescription,
saw_orderitemdep.DeptDescription,
saw_orderitem.GGroupDescription
FROM
saw_order
INNER JOIN saw_orderitem ON saw_order.Wo = saw_orderitem.Wo
INNER JOIN saw_orderitemdep ON saw_order.Wo = saw_orderitemdep.Wo
CROSS JOIN car_promisquestions ON car_promisquestions.Warehouse = saw_order.Warehouse
WHERE
saw_orderitemdep.Department IN (@Department) AND
@QDate
GROUP BY
saw_orderitemdep.Department ASC,
saw_order.Wo ASC,
saw_orderitem.Item ASC
ORDER BY
saw_orderitemdep.Department ASC,
saw_order.Wo ASC,
saw_orderitem.Item ASC,
saw_order.Date ASC
我遇到的问题是@QDate
似乎没有输出我期望的结果。如果我在设置SELECT @QDate
后运行@QDate
,我会得到结果saw_order.
日期> (@Due_Date - INTERVAL 5 DAY)
,如果我复制并粘贴代替我的WHERE中的@QDate
变量条款完美无缺。那么我做错了什么?为什么结果字符串有效但变量不起作用?