日期:2014-05-17  浏览次数:20432 次

在线请教查询的字段~~
已知A表的值有
id 地址1 地址2 地址3
002 guangzhou chengdu nanjing

如何查询在B表里面含有 地址1+地址2+地址3 (guangzhouchengdunanjing) 的字段

B表:
id ComAddress ComCity
A001 guang nan
A002 hubei dong
A003 chengdu xinjian
A004 xiaoxin linken
.  
.
.

最后找到的条件应该是B表里的
A001 guang nan

------解决方案--------------------
SQL code

create table A
(
id varchar(3),
地址1 varchar(30),
地址2 varchar(30),
地址3 varchar(30)
)

create table b
(
  id varchar(10),
  ComAddress varchar(30),
  ComCity varchar(30)
 
)

insert into A select '002', 'guangzhou', 'chengdu', 'nanjing'

insert into b 
select 'A001', 'guang', 'nan' union all
select 'A002', 'hubei', 'dong'  union all
select 'A003', 'chengdu', 'xinjian'  union all
select 'A004', 'xiaoxin', 'linken'  


select t1.* from b t1,A t2 where CHARINDEX(t1.ComAddress ,t2.地址1 + t2.地址2 + t2.地址3 ) >0 and CHARINDEX(t1.ComCity  ,t2.地址1 + t2.地址2 + t2.地址3 ) >0


id         ComAddress                     ComCity
---------- ------------------------------ ------------------------------
A001       guang                          nan

(1 行受影响)