有二张表,一张叫ta,一张叫tb。它们有uid与sdate这二个字段相对应的。现在有这三种情况,需要显示记录集:如果ta中在某个日期里面有记录,但是tb中没有,也要显示记录集,如果ta表中在某个日期下面,没有记录,而tb有,这个记录也要显示。如果ta表与tb表都有数据,这个也要显示出来。并且二个表中的uid一定是相同的,这个SQL语句如何写呢?我是这样写的,但是数据不对,有些字段没有值的,也显示值了,并且重复。SELECT a.uid, a.idate,a.step, a.eatcalory,a.actcalory,b.isj,b.isc FROM ta a, tb b WHERE a.uid = b.uid AND a.uid =845444请高手帮一下忙,加了DISTINCT 这个也是一样的结果。

解决方案 »

  1.   

    SELECT a.uid, a.idate,a.step, a.eatcalory,a.actcalory,b.isj,b.isc FROM  ta a left join  tb b on a.uid = b.uid where a.uid =845444union SELECT a.uid, a.idate,a.step, a.eatcalory,a.actcalory,b.isj,b.isc FROM  ta a right join  tb b on a.uid = b.uid where a.uid =845444
      

  2.   

     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  3.   

    回二楼,你的语句执行结果还是有重复,与我贴出来的代码执行结果相同。结果:uid   idate  eatcalory actcalory isj icj
    217780 2012-07-23 1236 5566 7788 1025 0 
    217780 2012-07-23 1236 5566 7788 975.76 0 
    217780 2012-07-22 7832 5555 6666 1025 0 
    217780 2012-07-22 7832 5555 6666 975.76 0 
    217780 2012-07-21 4731 3215 4683 1025 0 
    217780 2012-07-21 4731 3215 4683 975.76 0 
    217780 2012-07-20 NULL 6821 3287 1025 0 
    217780 2012-07-20 NULL 6821 3287 975.76 0 
    回三楼:我这是mysql数据库,其实我的要求也能清晰的表达出来。就是有二个表,查询二个表中数据,然后uid字段值是相同的,如果二个表中,只要有日期存在的数据就显示出来,不论是这二个表中哪一个表中有,或者都有,只是要求不能重复。比如第一个表中2012-1-1有数据,那就显示,不论第二个表中是否有此日期的数据,如果第二个表中有此日期的数据,那就一起显示。第二个表中如果有2012-2-1的数据,不论第一个表中是否有,都要显示,如果第一个表中,也有此日期下的数据,那就一起显示出来。
      

  4.   

    楼上请看这儿:
    A表结构与数据uid    idate        step    eatcalory    actcalory
    21990  2012-01-01   6544    1212         9878
    21990  2012-01-02   3212    0            0
    21990  2012-01-03   2154    2309         1345
    B表结构与数据uid    idate         isj    isc
    21990  2012-01-01    6677   8876
    21990  2012-01-04    5631   3216
    通过某一SQL语句,希望查询结果:uid    idate        step    eatcalory    actcalory    isj    isc
    21990  2012-01-01   6544    1212         9878         6677   8876
    21990  2012-01-02   3212    0            0              0      0
    21990  2012-01-03   2154    2309         1345           0      0
    21990  2012-01-04   0         0           0           5631   3216
      

  5.   

    select 
      isnull(a.uid,b.uid) as uid,
      isnull(a.idate,b.idate) as idate,
      isnull(a.step,0) as step,
      isnull(a.eatcalory,0) as eatcalory,
      isnull(a.actcalory,0) as actcalory,
      isnull(b.isj,0) as isj,
      isnull(b.isc,0) as isc
    from a
    full join b on a.uid=b.uid and a.idate=b.idate
      

  6.   

    select uid,idate,step,sum(eatcalory),sum(actcalory),sum(isj),sum( isc)
    form (
    select uid,idate,step,eatcalory,actcalory,0 as isj,0 as isc
    from a
    union all
    select uid,idate,0,0,0, isj, isc
    from b
    ) t
    group by uid,idate
      

  7.   


    这个怎么加uid=21990查询的条件?请指点。