两表的查询~~用子查询或者~用内链接和外连接~select * from table1 inner join table2 on table1.on=table2.on (内连)外联 select * from table left join table2 on .................子查询 假如table1 表中有字段 ontable2 中也有字段 on需要用ON 连接起来~~ select * from table1 where on=(select on from table2 )
declare @table1 table(Age int,labour int, sex varchar(10)) insert into @table1 values(20,10,'男') insert into @table1 values(21,8,'女') insert into @table1 values(22,11,'男') insert into @table1 values(23,8,'女') select * from @table1declare @table2 table(Age int,labour int, sex varchar(10),Field1 nvarchar(50),Field2 nvarchar(100)) insert into @table2 values(20,10,'男','测试1','测试1.1') insert into @table2 values(21,8,'女','测试2','测试2.1') insert into @table2 values(22,11,'男','测试3','测试3.1') insert into @table2 values(23,9,'女','测试4','测试4.1') insert into @table2 values(24,7,'女','测试5','测试5.1') insert into @table2 values(25,13,'男','测试6','测试6.1') select * from @table2 --按@table1查询 select * from @table2 where Age in (select Age from @table1) And labour in (select labour from @table1) And sex in (select sex from @table1)
--按@table1查询@table2,方式二 select AA.* From (select * From @table2 ) AS AA inner join (select * From @table1 Group By Age, labour,sex) As BB on (AA.Age=BB.Age And AA.labour=BB.labour And AA.sex=BB.sex)
select * from table1 a,table2 b where a.年龄=@年龄 and 劳动力=@劳动力 and 性别=@性别 and a.关联字段=b.关联字段
要是有关联字段那我还干嘛用这三个做为条件呀 哦还有点就是那个table1中年龄是(10-20)这样的格式table2年龄(10) 不管用什么方法也可以创建第三个表 急求高手解答?
两表的查询~~用子查询或者~用内链接和外连接~select * from table1 inner join table2
on table1.on=table2.on
(内连)外联
select * from table left join table2
on .................子查询
假如table1 表中有字段 ontable2 中也有字段 on需要用ON 连接起来~~
select * from table1 where on=(select on from table2 )
declare @table1 table(Age int,labour int, sex varchar(10))
insert into @table1 values(20,10,'男')
insert into @table1 values(21,8,'女')
insert into @table1 values(22,11,'男')
insert into @table1 values(23,8,'女')
select * from @table1declare @table2 table(Age int,labour int, sex varchar(10),Field1 nvarchar(50),Field2 nvarchar(100))
insert into @table2 values(20,10,'男','测试1','测试1.1')
insert into @table2 values(21,8,'女','测试2','测试2.1')
insert into @table2 values(22,11,'男','测试3','测试3.1')
insert into @table2 values(23,9,'女','测试4','测试4.1')
insert into @table2 values(24,7,'女','测试5','测试5.1')
insert into @table2 values(25,13,'男','测试6','测试6.1')
select * from @table2
--按@table1查询
select * from @table2 where Age in (select Age from @table1) And labour in (select labour from @table1) And sex in (select sex from @table1)
--按@table1查询@table2,方式二
select AA.* From (select * From @table2 ) AS AA
inner join (select * From @table1 Group By Age, labour,sex) As BB
on (AA.Age=BB.Age And AA.labour=BB.labour And AA.sex=BB.sex)