在主表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: @table1declare @table1 table ([well_id] bigint)insert into @table1select 677 union allselect 23434 union allselect 12 union allselect 1234577 union allselect 12345 union allselect 1234521 union allselect 456--> Test Data: @tabledeclare @table table ([a] varchar(100))insert into @tableselect 'policy_id=(×××××)+project_id=(×××××)+site_id=(×××××)+well_id=(12345)'--Code--select * from @table1--select * from @tabledeclare @id varchar(100)select @id=substring(a,charindex('well_id=(',a)+9,5) from @tableprint 'well_id='+@idselect * from @table1 where [well_id] like '%'+@id+'%'--Drop--Result/*well_id=12345well_id--------------------1234577123451234521*/