查询中的MYSQL循环变量

时间:2016-05-10 20:23:09

标签: mysql

我有一个ID列表,我想通过下面的查询循环。所以我希望查询为每个ID运行,并让ID_FROM_LIST从LIST_OF_IDS中提取下一个ID。

列表:

LIST_OF_IDS = (379, 517, 519, 797, 800, 896, 897, 900, 902, 903, 904, 905, 906, 907, 919)

查询:

    select w.Record_Count, w.Distinct_Provider_Count from  
(select  c as Record_Count, dc as Distinct_Provider_Count
from
(select * from centene_cic_decodes where standard_use in ('','P','PL')) r left join
(
select 
deceased_status as t, count(*) as c , count(distinct group_key) as dc,  'provider_status' as cat
from t_conditioncodes_0086252_cen_idvf2 
where NET_ID_ef = ID_FROM_LIST
group by 1 
union all 
select 
name_status  as t, count(*) as c , count(distinct group_key) as dc,  'name_status' as cat
from t_conditioncodes_0086252_cen_idvf2  
where NET_ID_ef = ID_FROM_LIST
group by 1
) z
on (if((z.t is null or z.t = ''), 'BLANK',z.t)) = r.Status_Codes and z.cat = r.Category 
order by r.category_rank, r.rank)w ;

目标是为每个ID创建单独的输出。因此有一份报告为379,另一份为517等报告......

(为简单起见,这只是完整查询和ID列表的一小部分。)

1 个答案:

答案 0 :(得分:0)

如果在表中引用了LIST_OF_IDS的所有id,则可以使用find_in_set循环逗号分隔列表(例如:" 1,2,3"):

select t.id id_from_list
from mytable t
where find_in_set(t.id, LIST_OF_IDS);