我想将数组设置为变量,以便在“IN”子句中的查询中使用它。下面的代码给我一个错误,所以我不确定它是否可能在mysql中。
set @ids = (1,2,3,6);
select
sum(promotion_id IN @ids AND confirmed_at IS NOT NULL)
from blabla
我得到两个不同的结果。也就是说,find_in_set和IN(@ids)给我相同的结果,但它与我从IN(1,2,3)获得的结果不同:
set @voda_prom ='1483, 2396, 2395, 1887';
SUM(FIND_IN_SET(promotion_id,@voda_prom) and confirmed_at IS NOT NULL)*1.0/ SUM(FIND_IN_SET(promotion_id,@voda_prom)) as p,
SUM(promotion_id IN(1483,2396,2395,1887) and confirmed_at IS NOT NULL)*1.0/ SUM(promotion_id IN(1483,2396,2395,1887)) as p_check,
SUM(promotion_id IN(@voda_prom) and confirmed_at IS NOT NULL)*1.0/ SUM(promotion_id IN(@voda_prom)) as p_1,
答案 0 :(得分:1)
使用find_in_set
代替in
set @ids = '1,2,3,6';
select
SUM(CASE WHEN FIND_IN_SET(promotion_id,@ids) AND confirmed_at IS NOT NULL THEN 1 ELSE 0 END)
from blabla
此函数与php
中的in_array
相同
答案 1 :(得分:0)
您可以这样使用:
SET @ids = '1, 2, 3, 6';
select
sum(`promotion_id` IN ( @ids) AND `confirmed_at` IS NOT NULL)
from `blabla`
答案 2 :(得分:0)
app.component.ts