sql关联查询、子查询综合应用

    技术2022-07-29  92

    一个主表,关联若干个子表,同时还要获取子表的标识信息,例如:人(刑满释放、易上访、重点青少年,吸毒人员,临时管控人员),应用场景就是,某某人有可能身兼数职(例如即是易上访、又是刑满释放、而且还是临时管控…) 可以试试如下的一摊代码,大多数都能看懂,欢迎提供更好的方式。

    select * from ( SELECT aids.population_code aids, correction.population_code correction,drug.population_code drug,teenagers.population_code teenagers,mental.population_code mental,released.population_code released,temp.population_code temp,visits.population_code visits,popubase.* FROM t_population_base popubase LEFT JOIN t_aids_population aids ON popubase.id = aids.population_code LEFT JOIN t_correction_population correction ON popubase.id = correction.population_code LEFT JOIN t_drug_population drug ON popubase.id = drug.population_code LEFT JOIN t_key_teenagers teenagers ON popubase.id = teenagers.population_code LEFT JOIN t_mental_disorders_population mental ON popubase.id = mental.population_code LEFT JOIN t_release_population released ON popubase.id = released.population_code LEFT JOIN t_temp_control_population temp ON popubase.id = temp.population_code LEFT JOIN t_visits_population visits ON popubase.id = visits.population_code) specialList where aids is not null or correction is not null or drug is not null or teenagers is not null or mental is not null or released is not null or temp is not null or visits is not null

    求更好的方式…

    Processed: 0.020, SQL: 9