表一:
    id    pian   bradn   mdel2
    1     君越    别克   SGM7
    2     凯美瑞  丰田   GTM
表二:
     id   brand  mdel     olume
      1    丰田  GTM7240G    20
      2    丰田牌  GTM7240G    2
      3    别克    SGM7280GL   30
      4    别克牌  SGM7280GL    2
现在想这样,出表1的pian和表2的sum(olume)条件是只要表2 brand类似 表1 brand or 表2 mdel 
结果:为
      
       君越        32
      凯美瑞      22
按sum(olume)从大到小输出10条,怎么写SQL语句呢,mysql 5.1版本,求各位给点参考啊,

解决方案 »

  1.   

    现在这台机没有MySQL..
    写个T-SQL版的给你.也一样的.create table tb1(
    id int,
    pian varchar(20),
    brand varchar(20),
    mdel2 varchar(20)
    )
    insert tb1 select 1,'君越','别克','SGM7'
    union all select 2,'凯美瑞','丰田','GTM'create table tb2(
    id int,
    brand varchar(20),
    mdel varchar(20),
    olume int
    )
    insert tb2 select 1,'丰田','GTM7240G',20
    union all select 2,'丰田牌','GTM7240G',2
    union all select 3,'别克','SGM7280GL',30
    union all select 4,'别克牌','SGM7280GL',2select a.brand,
    (select sum(olume) from tb2 b where charindex(a.mdel2,b.mdel)>0)
    from tb1 adrop table tb1,tb2
      

  2.   

    不得哦,好像MYSQL不支持charindex
      

  3.   

    select vcom_autocl.pinpai,
    (select sum(olume) as bb from vcom_autoph  where instr(vcom_autoph.brand,vcom_autocl.brand)>0
    )
    from vcom_autocl where py='4' 怎么对按sum(olume)从大到小排序呢?
      

  4.   

    select vcom_autocl.pinpai,
    (select sum(olume)  from vcom_autoph  where 
    instr(vcom_autoph.brand,vcom_autocl.pinpai)>0
    or instr(vcom_autoph.brand,vcom_autocl.brand)>0 or 
    case 
    when vcom_autocl.cl=2
    then instr(vcom_autoph.mdel,vcom_autocl.mdel1)
    when vcom_autocl.cl=3
    then instr(vcom_autoph.mdel,vcom_autocl.mdel1) and instr(vcom_autoph.mdel,vcom_autocl.mdel2)
    end
    ) from vcom_autocl where py='4'  order by 
    (select sum(olume) from vcom_autoph  where 
    instr(vcom_autoph.brand,vcom_autocl.pinpai)>0
    or instr(vcom_autoph.brand,vcom_autocl.brand)>0 ) desc
    case怎么没有启作用呢