两张表。
用户表:
id 用户名 标识
1 张三 1
2 李四 1
3 王五 2信息表
id 用户id 用户名 性别
1 1 张三 男
2 2 李四 男
3 3 王五 女----------------------------------------------------
条件是where 用户表.标识=1
我想查询出来的结果是用户ID 用户名 性别
1 张三 男
2 李四 男-----------------------------------------------------
请各位大神们指点,小弟不胜感激。

解决方案 »

  1.   

    用户表t_User
    信息表t_Info
    select A.ID as UserID,
    A.UserName,
    B.Sex
    from t_User A
    Inner Join t_Info On A.ID=B.UserID
    where A.Flag=1
      

  2.   


    --> 测试数据: @用户表
    declare @用户表 table (id int,用户名 varchar(4),标识 int)
    insert into @用户表
    select 1,'张三',1 union all
    select 2,'李四',1 union all
    select 3,'王五',2--> 测试数据: @信息表
    declare @信息表 table (id int,用户id int,用户名 varchar(4),性别 varchar(2))
    insert into @信息表
    select 1,1,'张三','男' union all
    select 2,2,'李四','男' union all
    select 3,3,'王五','女'--第一种思路
    select 
    b.用户id,b.用户名,b.性别 
    from @用户表 a left join @信息表 b on a.id=b.用户id 
    where a.标识=1--第二种思路
    select 
    b.用户id,b.用户名,b.性别 
    from @信息表 b 
    where (select 标识 from @用户表 a where a.id=b.用户id)=1/*
    用户id        用户名  性别
    ----------- ---- ----
    1           张三   男
    2           李四   男
    */
      

  3.   


    --> 测试数据: @用户表
    declare @用户表 table (id int,用户名 varchar(4),标识 int)
    insert into @用户表
    select 1,'张三',1 union all
    select 2,'李四',1 union all
    select 3,'王五',2--> 测试数据: @信息表
    declare @信息表 table (id int,用户id int,用户名 varchar(4),性别 varchar(2))
    insert into @信息表
    select 1,1,'张三','男' union all
    select 2,2,'李四','男' union all
    select 3,3,'王五','女'--用b.*的话,会多一个字段
    select b.* 
    from @用户表 a,@信息表 b 
    where a.id=b.用户id and a.标识=1/*
    id          用户id        用户名  性别
    ----------- ----------- ---- ----
    1           1           张三   男
    2           2           李四   男
    */
      

  4.   

    create table #user(
    id int null,
    username nvarchar(10) null,
     int null
    )
    create table #info(
    id int null,
    userid int null,
    username nvarchar(10) null,
    sex nvarchar(2) null
    )
    insert into #user values (1,'张三',1),(2,'李四',1),(3,'王五',2)
    insert into #info values (1,1,'张三','男'),(2,2,'李四','男'),(3,3,'王五','女')
    select a.userid 用户ID,a.username 用户名,a.sex 性别 from #info a
    inner join #user b on b.id=a.userid where b.=1
    两个表是根据用户表#user的id和信息表#info的userid的字段来连接的,然后,查询出用户表的标识等于1的记录,查询结果如下:
    用户ID 用户名 性别
    1 张三 男
    2 李四 男
      

  5.   

    Select 用户ID 用户名 性别
    From 用户表 T1 Left Join 信息表 T2 On (T1.ID = T2.用户id)
    Where 用户表.标识=1
      

  6.   

    不是join一下,然后where一下?
      

  7.   


    select a.id 用户ID,a.用户名,b.性别 from 用户表 a
    inner join 信息表 b on b.用户ID=a.id where a.标识=1
      

  8.   

    select 
        b.用户id,b.用户名,b.性别 
    from @用户表 a left join @信息表 b on a.id=b.用户id 
    where a.标识=1
      

  9.   

    select 
        b.用户id,b.用户名,b.性别 
    from @用户表 a left join @信息表 b on a.id=b.用户id 
    where a.标识=1
      

  10.   

    --> 测试数据: @用户表
    declare @用户表 table (id int,用户名 varchar(4),标识 int)
    insert into @用户表
    select 1,'张三',1 union all
    select 2,'李四',1 union all
    select 3,'王五',2--> 测试数据: @信息表
    declare @信息表 table (id int,用户id int,用户名 varchar(4),性别 varchar(2))
    insert into @信息表
    select 1,1,'张三','男' union all
    select 2,2,'李四','男' union all
    select 3,3,'王五','女'select b.用户id ,b.用户名 , b.性别  
    from @用户表 a,@信息表 b 
    where a.id=b.用户id and a.标识=1/*
    用户id        用户名  性别
    ----------- ---- ----
    1           张三   男
    2           李四   男
    */