I want to get only status = 1 records. But I don't have status column in my table. So I derived the value using CASE... WHEN... THEN. But when I try to use case in where clause, It shows syntax error.
我想只获得status = 1条记录。但我的表格中没有状态栏。所以我使用CASE ... WHEN ...那么得出了价值。但是当我尝试在where子句中使用case时,它显示语法错误。
my query
SELECT SQL_CALC_FOUND_ROWS *,
CASE
WHEN quantity > num_used AND (CURDATE() BETWEEN coupon_start_date AND coupon_end_date) THEN '1'
ELSE '0'
END AS STATUS
FROM
table_coupon_code
WHERE
(CASE
WHEN quantity > num_used AND (CURDATE() BETWEEN coupon_start_date AND coupon_end_date) THEN '1'
ELSE '0'
END AS STATUS) = '1' AND coupon_status <> '2'
How can I do this ?
我怎样才能做到这一点 ?
4 个解决方案
#1
16
remove AS STATUS
from where clause
从where子句中删除AS STATUS
SELECT SQL_CALC_FOUND_ROWS * ,
CASE WHEN quantity > num_used AND (CURDATE( ) BETWEEN coupon_start_date AND coupon_end_date)
THEN '1'
ELSE '0'
END AS STATUS
FROM table_coupon_code
WHERE
CASE WHEN quantity > num_used AND (CURDATE( ) BETWEEN coupon_start_date AND coupon_end_date)
THEN '1'
ELSE '0'
END = '1'
AND coupon_status <> '2'
But your CASE
is really unnecessary. Just use your CASE
condition as stand-alone WHERE
condition, like
但你的CASE真的没必要。只需将您的CASE条件用作独立的WHEREcondition,就像
[...]
WHERE quantity > num_used AND
CURDATE( ) BETWEEN coupon_start_date AND coupon_end_date AND
coupon_status <> '2'
#2
3
If you do not want to repeat the case statement you could wrap the select in a subselect or make a view. Subselect is something like
如果您不想重复case语句,可以将select包装在子选择中或创建视图。次选是类似的
select status
from (select case
when zip like '4321%' then 1 else 0 end as status
from adr
) t
where status = 1;
#3
1
Nobody Suggested the HAVING Clause?
没有人建议使用HAVING条款?
This allows you to query the selected columns, instead of the actual results. Great for using case and function calls.
这允许您查询选定的列,而不是实际结果。非常适合使用大小写和函数调用。
#4
0
I suppose you have some other, more complicated query, as the one you have provided is equivalent to:
我想你有一些其他更复杂的查询,因为你提供的查询相当于:
SELECT SQL_CALC_FOUND_ROWS * ,
'1' AS STATUS
FROM table_coupon_code
WHERE quantity > num_used
AND CURDATE() BETWEEN coupon_start_date AND coupon_end_date
AND coupon_status <> '2'
#1
16
remove AS STATUS
from where clause
从where子句中删除AS STATUS
SELECT SQL_CALC_FOUND_ROWS * ,
CASE WHEN quantity > num_used AND (CURDATE( ) BETWEEN coupon_start_date AND coupon_end_date)
THEN '1'
ELSE '0'
END AS STATUS
FROM table_coupon_code
WHERE
CASE WHEN quantity > num_used AND (CURDATE( ) BETWEEN coupon_start_date AND coupon_end_date)
THEN '1'
ELSE '0'
END = '1'
AND coupon_status <> '2'
But your CASE
is really unnecessary. Just use your CASE
condition as stand-alone WHERE
condition, like
但你的CASE真的没必要。只需将您的CASE条件用作独立的WHEREcondition,就像
[...]
WHERE quantity > num_used AND
CURDATE( ) BETWEEN coupon_start_date AND coupon_end_date AND
coupon_status <> '2'
#2
3
If you do not want to repeat the case statement you could wrap the select in a subselect or make a view. Subselect is something like
如果您不想重复case语句,可以将select包装在子选择中或创建视图。次选是类似的
select status
from (select case
when zip like '4321%' then 1 else 0 end as status
from adr
) t
where status = 1;
#3
1
Nobody Suggested the HAVING Clause?
没有人建议使用HAVING条款?
This allows you to query the selected columns, instead of the actual results. Great for using case and function calls.
这允许您查询选定的列,而不是实际结果。非常适合使用大小写和函数调用。
#4
0
I suppose you have some other, more complicated query, as the one you have provided is equivalent to:
我想你有一些其他更复杂的查询,因为你提供的查询相当于:
SELECT SQL_CALC_FOUND_ROWS * ,
'1' AS STATUS
FROM table_coupon_code
WHERE quantity > num_used
AND CURDATE() BETWEEN coupon_start_date AND coupon_end_date
AND coupon_status <> '2'