有两张表 
第一张表有字段hostcompanyid,name 还有其他一些无关的字段就不写出来了
第二张表有字段archivingdate,archivingCount,companyId.
其中hostcompanyid和companyId是对应关系的,
现在求一sql语句查询出每个表一里面所对应的archivingCount数量,如果数量
在第二张表中没有记录,那就在页面将archivingCount显示数量为0,有的话就显示
具体数量

解决方案 »

  1.   

    select companyId, name,ArchivingCount,RealtimeCount,ArchivingDate from global_company g, dashboardinfo d where d.ArchivingDate=curdate()-1 and d.companyId = g.hostingcompanyid order by name asc;
    是我目前的写发,但是只能查出在第二张表有对应数量的数据
    无法将没有对应数据的公司的数量设置成0!
      

  2.   

    select g.name,case when d.ArchivingCount is null then 0 else d.ArchivingCount end 'ArchivingCount' from global_company g left join dashboardinfo d on d.companyid=g.hostingcompanyid where d.ArchivingDate=curdate()-1 order by name asc;
      

  3.   

    select archivingCount(column,null,0,1) from table where companyId in{ };
    这个table为第二张表
    {}里面写一个查询语句,查出来的为第一张表hostcompanyid的集合
    {}中查询出来的范围是大约table中的,所以就会有的数据查询出来为null,这语句就是把空改为0的
      

  4.   

    即使第一张表有N多记录,但是第二张表只一条2楼给出的sql如果第二张表只一条记录也只能显示哪个存在第二张表的公司.
    其他的都查询不出来.
      

  5.   

    使用decode函数,decode(archivingCount,'',0,archivingCount)这样如果为空即返回0select a.hostcompanyid,a.name,decode(b.archivingCount,'',0,b.archevingCount) as archivingCount from table1 a,table2 b where a.hostcompanyid=b.companyId
    /
    代码可能字母有些不对,你检查一下,
      

  6.   

    select g.name,case when d.ArchivingCount is null then 0 else d.ArchivingCount end 'ArchivingCount' from global_company g left join (select * from dashboardinfo where ArchivingDate=curdate()-1) d on d.companyid=g.hostingcompanyid order by name asc;
      

  7.   

    用Left join ,和case when 语句--建立测试数据
    create table tb1
    (
    hostcompanyid int,
    name varchar(10)
    )insert into tb1 values (1,'a')
    insert into tb1 values (2,'b')
    insert into tb1 values (3,'c')
    insert into tb1 values (4,'e')create table tb2
    (
    archivingdate varchar(10),
     archivingCount int,
    companyId int
    ) insert into tb2 values ('20130101',33,1)
    insert into tb2 values ('20130101',22,2)
    insert into tb2 values ('20130101',11,3)---select a.hostcompanyid, 
    case 
    when b.archivingCount  IS NULL   then 0
    else b.archivingCount
    end archivingCount
    from tb1 a left join tb2 b 
    on a.hostcompanyid = b.companyId
      

  8.   

    SQL SERVER 数据库哦,如果是其他数据库 要改改 is NULL 语句
    也有可能left join语句不兼容 比如oracle 8i
      

  9.   


    我在MYSQL 5.2.44 数据库 上测试了一把 left join  和 is NULL 都是OK 的 可以直接用。
      

  10.   

    select isnull(b.archivingCount,0) from A a left join B b on a.hostcompanyid=b.companyId用left join就会包含左边表所有的记录了