包含某表字段内容的过滤

flyfranker 2011-09-15 01:17:42
sss表,tel字段,里面是11位手机号码,数据共560行,数据内容举例:13913801380
aaa表,phone字段,里面是包含上面这些手机号码,还有一些固定电话号码的内容,数据共7000行,数据内容举例:13913801380 020-56893685或020-56893685/,13913801380,数据格式不定,但肯定包含这些号码
需求:
需要把aaa表内,所有sss.tel中号码包含在aaa.phone内容中的行过滤出来,只要结果,不要排序。
...全文
225 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2011-09-15
  • 打赏
  • 举报
回复

select a.* from aaa a, sss s where charindex(a.phone , s.tel) > 0
flyfranker 2011-09-15
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 geniuswjt 的回复:]
SQL code

select phone from aaa where exists(
select 1 from sss where charindex(tel,aaa.phone)>0
)
--这样肯定没错了啊,如果有错就是你数据的问题
[/Quote]

哈哈,有效,满分!
快溜 2011-09-15
  • 打赏
  • 举报
回复
select distinct a.*
from aaa a,sss b
where a.phone like '%'+b.tel+'%'
NBDBA 2011-09-15
  • 打赏
  • 举报
回复
1楼JOIN方法容易产生重复,而且LIKE反了
NBDBA 2011-09-15
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 geniuswjt 的回复:]
SQL code

select phone from aaa where exists(
select 1 from sss where charindex(tel,aaa.phone)>0
)
--这样肯定没错了啊,如果有错就是你数据的问题
[/Quote]
果然,呵呵
NBDBA 2011-09-15
  • 打赏
  • 举报
回复
select phone from aaa where exists(
select 1 from sss where aaa.phone LIKE '%' + RTRIM(tel) + '%'
)
geniuswjt 2011-09-15
  • 打赏
  • 举报
回复

select phone from aaa where exists(
select 1 from sss where charindex(tel,aaa.phone)>0
)
--这样肯定没错了啊,如果有错就是你数据的问题
geniuswjt 2011-09-15
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 flyfranker 的回复:]
1楼的命令过滤出62000行,几乎是aaa表数据的10倍,而且所有行的phone字段全是空白
3楼的命令结果只有8行,其中7行的手机号码都是全角数字
4楼的命令没有任何结果
补充一点,sss.tel和aaa.phone都是varchar(50)

需求确实有点麻烦,需要反过来查询aaa表内,所有sss.tel中号码包含在aaa.phone内容中的行过滤出来
[/Quote]需求很简单啊,你上个10行左右测试数据吧
估计是你数据格式的问题
flyfranker 2011-09-15
  • 打赏
  • 举报
回复
1楼的命令过滤出62000行,几乎是aaa表数据的10倍,而且所有行的phone字段全是空白
3楼的命令结果只有8行,其中7行的手机号码都是全角数字
4楼的命令没有任何结果
补充一点,sss.tel和aaa.phone都是varchar(50)

需求确实有点麻烦,需要反过来查询aaa表内,所有sss.tel中号码包含在aaa.phone内容中的行过滤出来
--小F-- 2011-09-15
  • 打赏
  • 举报
回复
select phone from aaa t where exists(select 1 from sss where tel=t.phone)
koumingjie 2011-09-15
  • 打赏
  • 举报
回复

create table sss(tel varchar(11))
insert into sss

select '11111111111' union all
select '22222222222' union all
select '33333333333' union all
select '44444444444'


create table aaa(phone varchar(20))
insert into aaa

select '11111111111' union all
select '22222222222' union all
select '33333333333' union all
select '44444444444' union all
select '020-56893685' union all
select '13913801380' union all
select '020-56893685' union all
select '13913801380'


select * from aaa where phone in (select tel from sss)

phone
------------
11111111111
22222222222
33333333333
44444444444

chuanzhang5687 2011-09-15
  • 打赏
  • 举报
回复
+1[Quote=引用 1 楼 wufeng4552 的回复:]
SQL code
select a.*
from aaa a,sss b
where b.tel like '%'+a.phone+'%'
[/Quote]
饮水需思源 2011-09-15
  • 打赏
  • 举报
回复
select * from aaa where phone like '%+(select tel from sss)+%'
geniuswjt 2011-09-15
  • 打赏
  • 举报
回复

--貌似需求看反了,改下
select phone from aaa where exists(
select 1 from sss where tel=aaa.phone
)

[Quote=引用 2 楼 geniuswjt 的回复:]
SQL code

select tel from sss where exists(
select 1 from aaa where phone=sss.tel
)
[/Quote]
geniuswjt 2011-09-15
  • 打赏
  • 举报
回复

select tel from sss where exists(
select 1 from aaa where phone=sss.tel
)
水族杰纶 2011-09-15
  • 打赏
  • 举报
回复
select a.*
from aaa a,sss b
where b.tel like '%'+a.phone+'%'

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧