字符串“求和“sql语句怎样写?

jason1997 2004-06-08 08:45:31
数据表如下:
table a table b
people_id station_id | station_id station_name
01 1 1 物理教师
01 2 | 2 副校长
01 3 | 3 教研组长


table c
people_id people_name age ....
01 张三 40


现在想要如下结果,只用sql语句(不用存储过程)

people_id people_name age station ....
01 张三 40 物理教师、副校长、教研组长


谢谢,因为要用到通用的查询条件添加,所以不能用存贮过程。

...全文
227 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
shanlisong 2004-06-09
  • 打赏
  • 举报
回复
UP
huanyi 2004-06-09
  • 打赏
  • 举报
回复

用计算字段算了,呵呵
windindance 2004-06-08
  • 打赏
  • 举报
回复
用一条SQL 语句不可能。
可以用自定义函数:
create function func_StrCat
(@id varchar(10))
returns varchar(1000)
as
begin
declare @temp (value varchar(100), id int identity(1,1))
declare @s varchar(1000)
declare @n int
set @s = ''
insert @temp select station_name from b where station_id in(select station_id from a where people_id=@id)

while exists(select * from @temp)
begin
select top 1 @s =@s + value , @n = id from @temp
delete @temp where @n=id
end
return @s
end

select people_id, func_StrCat(people_id) from a group by people_id
jason1997 2004-06-08
  • 打赏
  • 举报
回复
eboywy(飞影) ,你的方法恐怕不行。

同一个人可能有多个岗位职务,要把这个人所有职务形成一个字符串,作为一个字段

谢谢你关注

eboywy 2004-06-08
  • 打赏
  • 举报
回复
select
a.person_id,
c.person_name,
c.age,
b.station

from a,b,c

where a.person_id=c.person_id and a.station_id=b.station_id
jason1997 2004-06-08
  • 打赏
  • 举报
回复
同一个人可能有多个岗位职务,要把这个人所有职务形成一个字符串,作为一个字段

谢谢你关注
ziqing 2004-06-08
  • 打赏
  • 举报
回复
不好意思!!很没看清你的意思!!呵呵
ziqing 2004-06-08
  • 打赏
  • 举报
回复
物理教师、副校长、教研组长
要把这三个合起来有什么条件吗?是不是每一条记录都合在一起呀!!

2,497

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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