if exists(select * from sysobjects where name='表1' and xtype='U') drop table 表1
if exists(select * from sysobjects where name='表2' and xtype='U') drop table 表2
GO
--生成测试用数据,为SELECT初始环境
CREATE TABLE 表1(Num int,Area varchar(10))
INSERT INTO 表1 SELECT 1300000,'北京'
UNION ALL SELECT 1300001,'北京'
UNION ALL SELECT 1300002,'北京'
CREATE TABLE 表2(ID int,Mobile bigint)
INSERT INTO 表2 SELECT 1,13000000000
UNION ALL SELECT 2,13000000001
UNION ALL SELECT 3,13000010000
UNION ALL SELECT 4,13000010002
UNION ALL SELECT 5,13000022000
UNION ALL SELECT 6,13500000155
select * from 表1
select * from 表2
GO
--此处为用来查询的SELECT语句
SELECT Mobile,Area
FROM 表1,表2 WHERE Cast(left(mobile,7)as int) % 表1.Num=0
GO
--删除测试散数据
DROP TABLE 表1,表2
/* -- 结果 --
Mobile Area
-------------------- ----------
13000000000 北京
13000000001 北京
13000010000 北京
13000010002 北京
13000022000 北京
Declare @t Table(num varchar(12),area varchar(10))
Insert @t Select '1300000','北京'
Union all Select '1300001','北京'
Union all Select '1300002','北京'
-----------------
Declare @t1 Table(Id Int,mobile varchar(20))
Insert @t1 Select 1,'13000000000'
Union all Select 2,'13000000001'
Union all Select 3,'13000010000'
Union all Select 4,'13000010002'
Union all Select 5,'13000022000'
Union all Select 6,'13500000155'
Select *
From @t1 A
Where Exists
( Select 1 From @t where num=left(mobile,7) )