这个查询语句怎么写?

Edison621 2005-01-31 01:11:51
有两个表A和B,B表实际上是把表A的Authors拆成一个名字对应一条记录。并且把Author中名字规范成汉字。
A表:
id Authors ……
-----------------------------------------------------
1 andy liu;eidson lee;andy yang ……
2 judy mu;joyce duan ……

B表
id Author FormatAuthor
---------------------------------------
1 andy 安迪
1 edison 爱迪生
1 andy 安迪
2 judy 茱迪
2 joyce 乔伊斯

如果给定“安迪”,要反过来去表A中查找原始的Authors,如果不去重,如下写:
select a.id,a.Authors from 表A a,表B b where a.ID=b.ID and b.FormatAuthor like '安迪'

那么会得到两条重复的记录如下
id Authors
----------------------------------------
1 andy liu;eidson lee;andy yang
1 andy liu;eidson lee;andy yang

当然,如果用distinct的话可以排除,不过Authors是ntext类型,不能使用distinct,而由于Authors字段有可能超出nvarchar的最大长度,所以也不能像下面这么写:
select distinct a.id,cast(a.Authors as nvarchar(8000)) from 表A a,表B b where a.ID=b.ID and b.FormatAuthor like '安迪'

还有什么其他的办法?谢谢
...全文
118 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
LIHY70 2005-01-31
  • 打赏
  • 举报
回复

select a.id,a.Authors from 表A a,(select distinct id from 表B b where b.FormatAuthor like '安迪')c where a.id=c.id
试试
jinbingg 2005-01-31
  • 打赏
  • 举报
回复
ding
xiaoxiangqing 2005-01-31
  • 打赏
  • 举报
回复
select a.id,a.Authors from 表A a,
(select id from 表B where FormatAuthor like '安迪' group by id) b
where a.ID=b.ID
didoleo 2005-01-31
  • 打赏
  • 举报
回复
1楼的可以了
xiaoxiangqing 2005-01-31
  • 打赏
  • 举报
回复
select id,Authors from 表A where id
in
(
select distinct a.id from 表A a,表B b where a.ID=b.ID and b.FormatAuthor like '安迪'
)
JafyLiu 2005-01-31
  • 打赏
  • 举报
回复
你只要distinct A表的id就可以了,用个子查询就可以。
select * from a where id in
(select distinct a.id from 表A a,表B b where a.ID=b.ID and b.FormatAuthor like '安迪')

34,575

社区成员

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

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