我总共有四张表联查,第一张是地区表,第二张是时间表,
第三张表是酒店表,第四张表是报价表,我是想根据地区,还有时间
查询出一月当中的酒店房间报价信息
这是第一张表:地区表
[code=sql]
CREATE TABLE [dbo].[AreaInfo](
[ID] [int] IDENTITY(1,1) NOT NULL,
[aname] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[pid] [int] NULL,
[isverify] [bit] NULL,
CONSTRAINT [PK_AreaInfo] PRIMARY KEY CLUSTERED
(
[ID] 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
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'酒店编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'AreaInfo', @level2type=N'COLUMN',@level2name=N'ID'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'酒店名称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'AreaInfo', @level2type=N'COLUMN',@level2name=N'aname'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'父级编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'AreaInfo', @level2type=N'COLUMN',@level2name=N'pid'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'是否显示,1显示,0隐藏' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'AreaInfo', @level2type=N'COLUMN',@level2name=N'isverify'
[/code]
第二张表时间表,
[code=sql]
CREATE TABLE [dbo].[DateTimeInfo](
[ID] [int] IDENTITY(1,1) NOT NULL,
[year] [int] NULL,
[mun] [int] NULL,
[day] [int] NULL,
[isred] [bit] NULL,
CONSTRAINT [PK_DateTimeInfo] PRIMARY KEY CLUSTERED
(
[ID] 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
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'是否为红色,1红色/0黑色' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'DateTimeInfo', @level2type=N'COLUMN',@level2name=N'isred'
[/code]
第三张表是酒店表,
[code=sql]
CREATE TABLE [dbo].[HotelInfo](
[ID] [int] IDENTITY(1,1) NOT NULL,
[aoneID] [int] NULL,
[atwoID] [int] NULL,
[EName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[Cname] [nvarchar](250) COLLATE Chinese_PRC_CI_AS NULL,
[star] [int] NULL,
[detail] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
CONSTRAINT [PK_HotelInfo] PRIMARY KEY CLUSTERED
(
[ID] 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
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'酒店编号 ' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HotelInfo', @level2type=N'COLUMN',@level2name=N'ID'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'地区编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HotelInfo', @level2type=N'COLUMN',@level2name=N'aoneID'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'英文名称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HotelInfo', @level2type=N'COLUMN',@level2name=N'EName'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'中文名称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'HotelInfo', @level2type=N'COLUMN',@level2name=N'Cname'
[/code]
第四张表是报价表,我想说一下的是报价表里面有两个外键,一个是
酒店编号,和时间编号
我现在就是一报价表为主表,然后酒店编号查询出酒店名称
作为行显示,然后日期作为列显示,对应过来的是报价,四张表的
字段可以看一下图片会更好一点
[img]http://images.cnblogs.com/cnblogs_com/axinno1/230138/r_Snap1.png[/img]
,希望得到大家的帮助啊!
这样的联合查询最简单的办法就是,把所有要查询的表都写出来,然后你在where条件里面再确定他们之间的联系。
select * from 主表A A,副表B B,副表C C,副表D D
where A.id = B.id and B.id =C.id and C.id =D.id
这是基本写法,然后根据这个再自己改下。
如果你想得到别人给你sql语句最好就是把数据库结构给列清楚点,你这样写建表的tSQL,看起来不方便。
同意楼上的,要把数据结构列清楚一点,要不然,别人看到你的一大堆东西,都不想看了。
比如: 用户表{id,name,age ,sex,address}最好写上注释,这样别人如果会,也比较快帮上你,嘻嘻,只是俺 的建议。。