日期:2014-05-18  浏览次数:20563 次

联合查询两张表
表结构如下
用户表
SQL code


/****** 对象:  Table [dbo].[T_UserInfo]    脚本日期: 02/13/2012 17:10:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[T_UserInfo](
    [UserId] [int] IDENTITY(1,1) NOT NULL,
    [UStatus] [bit] NOT NULL CONSTRAINT [DF_T_UserInfo_UStatus]  DEFAULT ((1)),
    [LoginName] [nvarchar](20) NOT NULL,
    [UserName] [nvarchar](50) NOT NULL,
    [LoginPwd] [nvarchar](64) NOT NULL,
    [Address] [nvarchar](150) NULL,
    [Email] [nvarchar](50) NULL,
    [Phone] [nvarchar](11) NOT NULL,
    [UserCardNo] [nvarchar](50) NULL,
    [UserCardPwd] [varchar](64) NULL,
    [UserCardId] [varchar](15) NULL,
    [Menager] [nvarchar](50) NULL,
    [Recommend] [varchar](20) NULL,
    [UserLevel] [int] NOT NULL CONSTRAINT [DF_T_UserInfo_UserLevel_1]  DEFAULT ((0)),
    [UserMoney] [decimal](18, 2) NOT NULL CONSTRAINT [DF_T_UserInfo_UserMoney_1]  DEFAULT ((0)),
    [EVvalue] [int] NOT NULL CONSTRAINT [DF_T_UserInfo_EVvalue_1]  DEFAULT ((0)),
    [EVMenager] [varchar](20) NULL,
    [RecommendNum] [int] NOT NULL CONSTRAINT [DF_T_UserInfo_RecommendNum_1]  DEFAULT ((0)),
    [BankName] [varchar](30) NULL,
    [BankNo] [nvarchar](20) NULL,
    [CpuNo] [varchar](50) NULL,
    [HardDiskId] [varchar](50) NULL,
    [InsertTime] [datetime] NOT NULL CONSTRAINT [DF_T_UserInfo_InsertTime_1]  DEFAULT (getdate()),
    [Bak] [nvarchar](50) NULL,
 CONSTRAINT [PK_T_UserInfo_1] PRIMARY KEY CLUSTERED 
(
    [UserId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF


日志表
SQL code


/****** 对象:  Table [dbo].[T_MoneyLog]    脚本日期: 02/13/2012 17:11:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[T_MoneyLog](
    [LogId] [int] IDENTITY(1,1) NOT NULL,
    [UserId] [int] NOT NULL,
    [UserLoginName] [varchar](20) NOT NULL,
    [Recommend] [varchar](20) NULL,
    [LogContent] [varchar](100) NULL,
    [Ip] [varchar](50) NULL,
    [Menager] [varchar](20) NULL,
    [InsertTime] [datetime] NOT NULL CONSTRAINT [DF_T_MoneyLog_InsertTime]  DEFAULT (getdate()),
 CONSTRAINT [PK_T_MoneyLog] PRIMARY KEY CLUSTERED 
(
    [LogId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF



现在需要的是 把用户表里的phone、inserttime跟日志表里的ip、inserttime


------解决方案--------------------
select *
from tb1 a join tb2 b on a.关联字段 = b.关联字段
where 其他条件