表1.
[Table1_ID]    [Area_ID]    [emp_id]
1               1            10
2               3            22
3               7            22
4               8            10
5               9            22表2.
[ID]    [emp_id]     [Grand_Area]
1       10           1,3,7
2       22           3,7
效果.
[Table1_ID]     [Area_id]    [emp_id]   
1                1            10
2                3            22
3                7            22用一句SQL语句来表达意思.
select a.*  from table1 a where a.Area_id in (select b.Grand_Area from table2 b where b.emp_id=a.emp_id)就是这个意思, 但是这句SQL不能达到要求...
因为后面一个 (select b.Grand_Area from table2 b where b.emp_id=a.emp_id)
得到的Grand_Area 的值会被看做一个整体... 而没办法用来in...我表达能力不好.
希望各位大大能明白...

解决方案 »

  1.   

    select a.*  from table1 a, table2 b Where b.emp_id=a.emp_id and CharIndex(',' + Cast(a.Area_ID As Varchar) + ',', ',' + B.Grand_Area + ',') > 0
      

  2.   

    或者Likeselect a.*  from table1 a, table2 b Where b.emp_id=a.emp_id and ',' + b.Grand_Area + ',' Like '%,' + Cast(a.Area_ID As Varchar) + ',%'
      

  3.   

    再或者select a.*  from table1 a Inner Join  table2 b On b.emp_id=a.emp_id and CharIndex(',' + Cast(a.Area_ID As Varchar) + ',', ',' + B.Grand_Area + ',') > 0select a.*  from table1 a Inner Join  table2 b On b.emp_id=a.emp_id and ',' + b.Grand_Area + ',' Like '%,' + Cast(a.Area_ID As Varchar) + ',%'
      

  4.   

    再再或者:)
    select a.* from table1 a Inner Join table2 b On b.emp_id=a.emp_id and len(',' + B.Grand_Area + ',')>len(replace(',' + B.Grand_Area + ',',',' + ltrim(a.Area_ID) + ',', ''))
      

  5.   

    select a.*  from table1 a Inner Join  table2 b On b.emp_id=a.emp_id and CharIndex(',' + Cast(a.Area_ID As Varchar) + ',', ',' + B.Grand_Area + ',') > 0的执行效率最高吧?
    如果数据量很大的话...?