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

这样复杂的多表联合,怎么写sql语句?谁帮我看看,谢谢!
在主表table中有这么一个字段a,字段的值包含四个ID,对应四张附表,类似a的值是policy_id=(×××××)+project_id=(×××××)+site_id=(×××××)+well_id=(×××××)
查询条件的既有主表中的,也有附表中的,查询条件既可以是多个,也可以是单个,主表对附表是多对1。查询结果中的字段既有主表中的字段和附表中的字段。
比如:有查询条件1(附表table1[含有well_id字段]中的某一个字段b的模糊值)和查询条件2(主表中的某一个字段c的模糊值),其中主表通过字段a与附表table1的字段well_id关联,即附表中的well_id等于主表中字段a中well_id值,这样的sql语句怎么写?
各位不知道我说明白了没有,有谁能帮助我一下!

------解决方案--------------------
MS比较复杂-_-
给点测试数据和结果好理解(简化过的)
------解决方案--------------------
明白才怪,上午本来头就晕,能看完就不错了
------解决方案--------------------
好晕~~
------解决方案--------------------
..........
------解决方案--------------------
SQL code
--> Test Data: @table1
declare @table1 table ([well_id] bigint)
insert into @table1
select 677 union all
select 23434 union all
select 12 union all
select 1234577 union all
select 12345 union all
select 1234521 union all
select 456
--> Test Data: @table
declare @table table ([a] varchar(100))
insert into @table
select 'policy_id=(×××××)+project_id=(×××××)+site_id=(×××××)+well_id=(12345)'

--Code
--select * from @table1
--select * from @table

declare @id varchar(100)
select @id=substring(a,charindex('well_id=(',a)+9,5) from @table
print 'well_id='+@id
select * from @table1 where [well_id] like '%'+@id+'%'
--Drop

--Result
/*
well_id=12345
well_id
--------------------
1234577
12345
1234521
*/