结构如下:
表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:
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...求解!
解决方案 »
- 如何使用T-SQL对EXCEL报表进行操作(drop table)
- 如何查询每天新增的用户
- collate Chinese_PRC_CI_AS
- 我想学习数据库相关开发知识,请高手给与指点。
- 关于output的长度问题
- ++++++++++++++++++++++++QQ群:21389130 MS SQL Server开发者 请加入!++++++++++++++++++++++++
- 在sql server的触发器中如何定义一个变长的int型数组?
- SQL SERVER服务管理器启动
- 菜鸟的问题:各位高手们,能不能告诉我什么在什么情况下用inner join、在什么情况下用left join???
- 求一个查询
- SQL语法问题
- 创建数据库与表的批处理文件?
-- 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 行受影响)
*/
select a.code,b.* from 表1 as a,表2 as b where a.id=b.m_id
(select a.code from a where a.id = b.m_id) as code , id , pic , m_id ,age
from b