table1字段
bh    name 
100   张三
200   李四
300   王五
table2 
字段bh    age 
100   55
200   60我想得到的结果是bh    name    age 
100   张三     55
200   李四     60
300   王五这样一个查询

解决方案 »

  1.   

    select a.bh,a.name,b.age from ta a left join tb b on a.bh=b.bh
      

  2.   


    select a.*,isnull(ltrim(b.age),'')
     from table1 a left join table2 b on a.bh=b.bh
      

  3.   

    ----------------------------------------------------------------------------------
    -- Author : htl258(Tony)
    -- Date   : 2010-05-18 16:05:27
    -- Version: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
    --          Jul  9 2008 14:43:34 
    --          Copyright (c) 1988-2008 Microsoft Corporation
    --          Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
    -- Blog   : http://blog.csdn.net/htl258
    ------------------------------------------------------------------------------------> 生成测试数据表: [ta]
    IF OBJECT_ID('[ta]') IS NOT NULL
    DROP TABLE [ta]
    GO
    CREATE TABLE [ta] ([bh] [int],[name] [nvarchar](10))
    INSERT INTO [ta]
    SELECT '100','张三' UNION ALL
    SELECT '200','李四' UNION ALL
    SELECT '300','王五'--> 生成测试数据表: [tb]
    IF OBJECT_ID('[tb]') IS NOT NULL
    DROP TABLE [tb]
    GO
    CREATE TABLE [tb] ([bh] [int],[age] [int])
    INSERT INTO [tb]
    SELECT '100','55' UNION ALL
    SELECT '200','60'--SELECT * FROM [ta]
    --SELECT * FROM [tb]-->SQL查询如下:
    select a.bh,a.name,b.age from ta a left join tb b on a.bh=b.bh
    /*
    bh          name       age
    ----------- ---------- -----------
    100         张三         55
    200         李四         60
    300         王五         NULL(3 行受影响)
    */
      

  4.   

    NULL的处理在程序上做一下就好了,不推荐在SQL中做
      

  5.   


    select a.*,b.age
    from table1 a left join  table2 b on a.bh=b.bh
      

  6.   

    create table a (bh int,name  varchar(10))
    insert into a values(100 ,'张三')
    insert into a values(200 ,'李四')
    insert into a values(300 ,'王五')
    create table b(bh int,age int)
    insert into b values(100 ,55)
    insert into b values(200 ,60)
    go
    --方法1
    select a.bh,
           a.name,
           isnull(b.age,0) age
    from a full join b 
    on a.bh = b.bh
    order by a.bh--方法2
    select isnull(a.bh,b.bh) bh,
           isnull(a.name,'') name,
           isnull(b.age,0) age
    from a full join b 
    on a.bh = b.bh
    order by a.bhdrop table a , b/*
    bh          name       age         
    ----------- ---------- ----------- 
    100         张三         55
    200         李四         60
    300         王五         0(所影响的行数为 3 行)
    */
      

  7.   

    create table a (bh int,name  varchar(10))
    insert into a values(100 ,'张三')
    insert into a values(200 ,'李四')
    insert into a values(300 ,'王五')
    create table b(bh int,age int)
    insert into b values(100 ,55)
    insert into b values(200 ,60)
    go
    --方法1
    select a.bh,
           a.name,
           isnull(ltrim(b.age),'') age
    from a full join b 
    on a.bh = b.bh
    order by a.bh--方法2
    select isnull(a.bh,b.bh) bh,
           isnull(a.name,'') name,
           isnull(ltrim(b.age),'') age
    from a full join b 
    on a.bh = b.bh
    order by a.bhdrop table a , b/*
    bh          name       age          
    ----------- ---------- ------------ 
    100         张三         55
    200         李四         60
    300         王五         (所影响的行数为 3 行)
    */
      

  8.   


    select a.*,isnull(b.age,'')
    from table1 a left join table2 b on a.bh=b.bh