Select L.lastid,f.firstid,(Select 姓名+':'+电话+',' from [last] where firstid=f.firstid For XML Path('')) from [first] F left join [last] L on F.firstid=L.firstid Group by L.lastid,f.firstid参考技术A参考这个:
--1. 创建处理函数 CREATE FUNCTION dbo.f_strUnite(@id varchar) RETURNS varchar(8000) AS BEGIN DECLARE @str varchar(8000) SET @str = '' SELECT @str = @str + ',' + value FROM tabname WHERE user=@id RETURN STUFF(@str, 1, 1, '') END GO -- 调用函数 SELECt user, tfbm= dbo.f_strUnite(user) FROM user GROUP BY user
sqlserver数据库查询数据
数据查询-列别名 //使用AS来命名列
SELECT SCode AS 学生编号,SName AS 学生姓名,
SAddress AS 学生地址
FROM Students
WHERE SAddress <> ‘河南新乡‘ //使用=来命名列
SELECT 姓名 = FirstName+‘.‘+LastName
FROM Employees 注意:
1. + 连接的数据类型必须兼容
2. 如果 + 连接字符型数据,结果为字符串数据的连接
3. 如果 + 连接数值型数据,结果为数值的和
数据查询-限制行数 //限制固定行数
SELECT TOP 5 SName, SAddress
FROM Students WHERE SSex = 0
//返回百分之多少行
SELECT TOP 20 PERCENT SName, SAddress
FROM Students WHERE SSex = 0
查询单列排序
//升序排列
SELECT StudentID AS 学生编号,(Score*0.9+5) AS 综合成绩
FROM Score
WHERE (Score*0.9+5)>60
ORDER BY Score
//降序排列 SELECT StudentID,Score FROM Score ORDER BY Score DESC //按多列排序
SELECT StudentID AS 学生编号, CourseID AS 课程ID, Score AS 成绩
FROM Score
WHERE Score > 60
ORDER BY CourseID, Score