2016-05-10 81 views
0

我有一個我想循環查看下面的ID的列表。所以我希望查詢針對每個ID運行,並讓ID_FROM_LIST從LIST_OF_IDS中拉出下一個ID。查詢中的MYSQL循環變量

列表:

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爲簡單列表的片段。)

+0

你有沒有關閉的選擇...和你是什麼人循環變量裏面的查詢... sql qhery沒有循環...最終plsql有循環..解釋更好的目標..並檢查你的代碼.. – scaisEdge

+0

我的意思是有查詢運行一遍又一遍,代替ne xt ID每次。 – user3670277

回答

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);