我有3张表:
a: uid,xm,xb,age,sj,qq
b: uid,money,zt
c: uid,tdate,msxx如果个人填了 xm,就查出这个xm的所有个人资料在a中,如果选了b中money也能找出a中符合money值的数据,如果选了b中zt,c中msxx还是可以找到a中符合要求的个人信息。我这样写的: select aa.uid,aa.xm,aa.xb,aa.age,aa.sj,aa.qq,bb,uid,bb.money,bb.zt,cc.uid,cc.tdate,cc.msxx from a as aa,b as bb,c as cc where aa.uid=bb.uid or aa.uid=cc.uid or aa.uid=cc.uid;--------当然这里还需要加上另一个查询条件,我不知道该怎么写这个查询了,无论怎么写查出的都是空这样写的结果只出来4条,实际上应该是22条。

解决方案 »

  1.   


    declare @a table(uid int,xm varchar(12),xb char(2),age int,sj varchar(12),qq varchar(15))
    declare @b table(uid int,money decimal(10,2),zt varchar(12))
    declare @c table(uid int,tdate datetime,msxx varchar(12))insert @a select 
    1,'张三','男',29,'13813990000','1231414' union all select
    2,'张按时','女',25,'42340000','fsdf14' union all select
    3,'果然三','男',22,'ffff00fff','ggggg14'insert @b select 
    2,5511.46,'工资'union all select
    1,1515.4,'奖金'union all select
    3,3155.5,'工资'
    insert @c select
    1,'2009-08-01','asf as 'union all select
    2,'2009-04-21','fsfsf'union all select
    3,'2009-06-30','sgsg'select a.* from @a a
    join @b b on a.uid=b.uid
    join @c c on a.uid=c.uid
    where xm='张三'or money=1515.4 or zt='奖金' or msxx='sgsg'uid         xm           xb   age         sj           qq              
    ----------- ------------ ---- ----------- ------------ --------------- 
    1           张三           男    29          13813990000  1231414
    3           果然三          男    22          ffff00fff    ggggg14(所影响的行数为 2 行)
      

  2.   

    select * from a ,b ,c where a.uid=b.uid and b.uid=c.uid  and xm="XXX"
    select * from a ,b ,c where a.uid=b.uid and b.uid=c.uid  and money="XXX"
      

  3.   

    js_szy----->那如果出现有a表中xm存在,但在b、c表中都没有和a相应的数据,join on这块要怎么写了。
      

  4.   

    guguda2008-------->我现在要写一个多表选择性查询的问题,就是说3张表有可能选择其中一个字段查,也有可能是2个字段或者3个,它们的共性在于有相同的uid
      

  5.   

    select * from @a  WHERE xm='张三'
    UNION 
    select a.* from @a a
    join @b b on a.uid=b.uid
    join @c c on a.uid=c.uid
    where money=1515.4 or zt='奖金' or msxx='sgsg'uid         xm           xb   age         sj           qq              
    ----------- ------------ ---- ----------- ------------ --------------- 
    1           张三           男    29          13813990000  1231414
    3           果然三          男    22          ffff00fff    ggggg14(所影响的行数为 2 行)
      

  6.   

    我是说,如果只有a表中有数据,b、c中此时都没有数据的情况下,别人查xm 
    join @b b on a.uid=b.uid
    join @c c on a.uid=c.uid
    这个是不可能成立的~~~~~~
      

  7.   

    -- b,c表都没有记录,6楼的代码也可以运行
    -- 好多东西,只要稍加修改,就可以运用
    declare @a table(uid int,xm varchar(12),xb char(2),age int,sj varchar(12),qq varchar(15))
    declare @b table(uid int,money decimal(10,2),zt varchar(12))
    declare @c table(uid int,tdate datetime,msxx varchar(12))insert @a select 
    1,'张三','男',29,'13813990000','1231414' union all select
    2,'张按时','女',25,'42340000','fsdf14' union all select
    3,'果然三','男',22,'ffff00fff','ggggg14'select * from @a  WHERE xm='张三'
    UNION 
    select a.* from @a a
    join @b b on a.uid=b.uid
    join @c c on a.uid=c.uid
    where money=1515.4 or zt='奖金' or msxx='sgsg'
      

  8.   

     aa.uid=bb.uid or aa.uid=cc.uid or aa.uid=cc.uid把or改成and