在BOM表A中
成品料号  材料料号现在怎么样根据一份材料清单,查出这份材料清单是否有成品使用?或者说查出这份材料清单是哪一个成品使用?

解决方案 »

  1.   

    select 成品料号 from A where 材料料号 = '材料清单中的材料料号'
      

  2.   

    select case when 成品料号 is null then '无' else '有成品使用' end,材料料号 from A Inner Join 清单表 B On a.材料料号=b.材料料号
      

  3.   

    To:chuifengde
      你写的SQL语句,是反映这个清单里的材料被哪些成品使用.我的要求不是这样子,我要求的意思是也可以说:有哪个成品用的材料跟材料清单里的材料一模一样
      

  4.   

    --个人以为,还是用存储过程好些.--建立测试表
    create table ta(cp int,cl int)  --成品组成表
    create table tb(cl int)         --材料清单表
    insert ta 
    select 1,1
    union select 2,2
    union select 2,3
    union select 3,1
    union select 3,2
    union select 3,3
    union select 3,4
    union select 4,1
    union select 4,2insert tb
    select 2
    union select 3
    select distinct cp as 符合成品号  from ta where
    cp not in(select a.cp from ta a 
     where  not exists(select 1 from 
    ( select distinct b.cp,c.cl from (select cp,count(cl) as sl from ta group by cp) b,tb c
      where b.sl=(select count(cl) from tb)) d
       where d.cp=a.cp and d.cl=a.cl))
    --删除测试数据
    drop table ta
    drop table tb符合成品号       
    ----------- 
    2