select count(*) from ods_hx_ln_mst 
where (select count(*) from ods_hx_ln_reg
 where ods_hx_ln_reg.ac_id =  ods_hx_ln_mst.ac_id 
group by ods_hx_ln_mst.ac_id)>1以上语句是测试有多少条数据属于ods_hx_ln_mst对ods_hx_ln_reg是一对多的关系,但是能解释一下流程么?我仿照上面的,写了单个表的类似功能,想查某个表的某个字段存在2个以上数据的个数:select count(*) from ods_hx_cif_basic_inf 
where  (select count(*) from ods_hx_cif_basic_inf group by cif_id)>1;但是报错,说我不能插入多行的值……   求解

解决方案 »

  1.   

    插入多行的值? 你有insert吗?
      

  2.   

    select count(*) from ods_hx_cif_basic_inf  group by cif_id having count(cif_id) > 1
      

  3.   


    select cif_id,count(*) from ods_hx_cif_basic_inf group by cif_id having(Count(*)>1)
    or
    select count(*) from ods_hx_cif_basic_inf  
    where (select count(*) from ods_hx_cif_basic_inf 
           where ods_hx_cif_basic_inf.cif_id=cif_id
           group by cif_id)>1;
      

  4.   

    select count(*) from ods_hx_cif_basic_inf group by cif_id  这个查出来的是什么??看清楚了哦  好像你条件没有带上!
      

  5.   


    --你少了外层连接条件cif_id
    select cif_id,count(*) from ods_hx_cif_basic_inf group by cif_id having(Count(*)>1)
    or
    select count(*) from ods_hx_cif_basic_inf  a
    where (select count(*) from ods_hx_cif_basic_inf 
           where a.cif_id=cif_id
           group by cif_id)>1;
      

  6.   

    一般是用having方式,gelon修改的方法也可以哦
      

  7.   


    ---第一个修改
    select count(*) from ods_hx_cif_basic_inf  
    where (select count(*) from ods_hx_cif_basic_inf where ods_hx_ln_reg.ac_id = ods_hx_ln_mst.ac_id )>1;---第二个select count(*) from ods_hx_cif_basic_inf  
    group by cif_id having count(*)>1
      

  8.   

    查询ods_hx_ln_mst表中字段ac_id>1的记录数
    SELECT ac_id  FROM ods_hx_ln_mst  GROUP BY ac_id  HAVING COUNT(*)>1
      

  9.   

    你的关联条件明显和例句不一样
    select count(*) from ods_hx_cif_basic_inf 
    where (select count(*) from ods_hx_cif_basic_inf group by cif_id)>1;