各位大虾,小弟数据库方面不太熟悉,现在业务上碰到了个比较棘手的问题,需要写个sql查询,如下:
   假设我现在有三张表: A表 B表 C表               A表:
                    id    厂商名称    产品名  ....
                     1     康师傅      红茶   ....
                     2     统一        红茶   ....
                     3     汇源        红茶   ....               B表:
                    id    厂商名称    产品名  ....
                     1     统一        大骨面   ....
                     2     统一        米粉 ....
                     3     康师傅      大骨面....               C表:
                    id     厂商名称    产品名 ....
                     1      康师傅       鲜橙多 ....
                     2      康师傅      鲜橙多 ....
          
   .............. 写这么点都快累死我了,费脑筋...呵呵    然后现在我想要的是查询出来这样的结果 :               康师傅
               统一
               汇源 大家可能都明白了,我就是想要三张表中所有的厂商的名称,而且不需要重复的,希望大家支支招...拜谢喽~!
                                  

解决方案 »

  1.   

    select 厂商名称 from A
    union
    select 厂商名称 from B
    union
    select 厂商名称 from C
    ;
      

  2.   

    seelct distinct 厂商名称 from (
    select 厂商名称 from A
    union
    select 厂商名称 from B
    union
    select 厂商名称 from C);
      

  3.   

    一楼的可以实现了,3楼的distinct多余不
      

  4.   

    一楼和三楼的都可以,过滤出来的数据是一样的啦~呵呵  
       但是我还有个问题,我想再给这条语句 加个条件  就是当三张表里的 相同字段 parentID  的值都为 111
        我改怎么增加这个条件呢? 望指教
      

  5.   

    谢谢大家了,出来了 呵呵 ...........泪流满面.....
       语句就是根据大家的帮忙写的:
    select DISTINCT provider_name FROM(select brd.provider_name from BTS_REQ_DETAIL  brd
    where brd.cp_req_order_id = 10298
    union
    select bsc.provider_name from BSC_REQ_DETAIL bsc
    where bsc.cp_req_order_id = 10298
    union
    select tra.provider_name from train_req_detail tra
    where tra.cp_req_order_id = 10298
    )
    ;
      

  6.   

    为了省事的话可以按3楼的办法,在外层写上该条件过滤
    用1楼的写法,每个表里都写上where parentid='111'性能会好得多
      

  7.   

    select 厂商名称 from A where parentID = 111
    union
    select 厂商名称 from B where parentID = 111
    union
    select 厂商名称 from C where parentID = 111