结构如下:
表1:
    ID  NAME  CODE  ADDR
     1   yo     003   a
     2   ho     004    b
     3   he     002    c
表2:
    id  pic   m_id   age
     1   2     3      11
     2   3     3      14
    3   4      1       44获取表2中的所有信息,在根据表2中的m_id匹配表1中的ID,并得到表1的CODE。。最终数据是表1.code,表2.id,表2.pic,表2.m_id,表2.age...求解!

解决方案 »

  1.   

    select a.code,b.* from 表1 as a,表2 as b where a.id=b.m_id ?
      

  2.   

    select 1.code,2.* from 1 left join 2 on 1.ID=2.M_ID 
      

  3.   

    select A.code,B.* from 2 as B left join 1 as A on A.ID=B.M_ID 
      

  4.   

    select a.code,b.* from 表1 as a,表2 as b where a.id=b.m_id
      

  5.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-09-07 17:37:59
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[表1]
    if object_id('[表1]') is not null drop table [表1]
    go 
    create table [表1]([ID] int,[NAME] varchar(2),[CODE] varchar(3),[ADDR] varchar(1))
    insert [表1]
    select 1,'yo','003','a' union all
    select 2,'ho','004','b' union all
    select 3,'he','002','c'
    --> 测试数据:[表2]
    if object_id('[表2]') is not null drop table [表2]
    go 
    create table [表2]([id] int,[pic] int,[m_id] int,[age] int)
    insert [表2]
    select 1,2,3,11 union all
    select 2,3,3,14 union all
    select 3,4,1,44
    --------------开始查询--------------------------
    select a.code,b.* from 表1 as a,表2 as b where a.id=b.m_id
    ----------------结果----------------------------
    /* code id          pic         m_id        age
    ---- ----------- ----------- ----------- -----------
    002  1           2           3           11
    002  2           3           3           14
    003  3           4           1           44(3 行受影响)
    */
      

  6.   


    select a.code,b.* from 表1 as a,表2 as b where a.id=b.m_id
      

  7.   


    (select a.code from a where a.id = b.m_id) as code  , id , pic , m_id ,age 
    from b