朋友我被困在存储过程中。我无法显示不同的记录。
我有一个名字USERS
和Photos
。两者都是使用userID
的内连接。
由于表Photos
有多条记录,因此重复搜索结果。朋友可以帮我解决这个问题吗?
存储过程:
ALTER PROCEDURE dbo.homesearch
@gender varchar(6),
@age int,
@age2 int,
@religion varchar(50),
@status varchar(11),
@Country varchar(50),
@City varchar(50)
AS
SELECT
users.userID, photos.filename, users.userName,
users.FirstName, users.LastName, users.nationality,
users.Country, users.City, users.age, users.status
FROM
users
INNER JOIN
photos ON users.userID = photos.userID
WHERE
(users.age >= @age) AND (users.age <= @age2)
AND (users.gender = @gender) AND (users.religion = @religion)
AND (users.status = @status) AND (users.Country = @Country)
AND (users.City = @City)
RETURN
答案 0 :(得分:1)
您可以使用CROSS APPLY
SELECT u.userID
,u.userName
,P.filename
,u.FirstName
,u.LastName
,u.nationality
,u.Country
,u.City
,u.age
,u.status
FROM users u
CROSS APPLY
(
SELECT TOP 1 photos.filename
FROM photos
WHERE u.userID = userID
ORDER BY photos.SomeColumn --<-- Pick a column here
)p
WHERE (u.age >= @age) AND (u.age <= @age2)
AND (u.gender = @gender) AND (u.religion = @religion)
AND (u.status = @status) AND (u.Country = @Country)
AND (u.City = @City)
从用户表格中的每个用户中选择一个您想要从照片表中选择哪个TOP 1文件名的列。
或使用CTE
;With P_CTE
AS
(
SELECT TOP UserID, photos.filename,
rn = ROW_NUMBER() OVER (PARTITION BY UserID ORDER BY UserID)
FROM photos
)
SELECT users.userID, P_CTE.filename, users.userName,
users.FirstName, users.LastName, users.nationality,
users.Country, users.City, users.age, users.status
FROM users INNER JOIN P_CTE
ON users.userID = P_CTE.userID
WHERE (users.age >= @age) AND (users.age <= @age2)
AND (users.gender = @gender) AND (users.religion = @religion)
AND (users.status = @status) AND (users.Country = @Country)
AND (users.City = @City)
AND P_CTE.rn = 1
答案 1 :(得分:0)
修改您的选择查询: -
SELECT users.userID, photos.filename, users.userName,
users.FirstName, users.LastName, users.nationality,
users.Country, users.City, users.age, users.status
FROM users INNER JOIN photos
ON users.userID = photos.userID
WHERE (users.age >= @age) AND (users.age <= @age2)
AND (users.gender = @gender) AND (users.religion = @religion)
AND (users.status = @status) AND (users.Country = @Country)
AND (users.City = @City)
GROUP BY users.userID, photos.filename, users.userName,
users.FirstName, users.LastName, users.nationality,
users.Country, users.City, users.age, users.status
它将返回独特的结果。
答案 2 :(得分:0)
SELECT users.userID, photos.filename, users.userName,
users.FirstName, users.LastName, users.nationality,
users.Country, users.City, users.age, users.status
FROM users INNER JOIN (SELECT DISTINCT userID FROM photos) AS P
ON users.userID = P.userID
答案 3 :(得分:0)
这里的想法是减少你的照片表中的条目,因为每个用户都有很多照片。在我的示例中,我使用max(filename)
来获取名称最长的名称,但您可以在此处使用其他条件:
SELECT users.userID, P.filename, users.userName,
users.FirstName, users.LastName, users.nationality,
users.Country, users.City, users.age, users.status
FROM users INNER JOIN
(SELECT userID, max(filename) as filename
FROM photos
GROUP BY userID) AS P
ON users.userID = P.userID
WHERE (users.age >= @age) AND (users.age <= @age2)
AND (users.gender = @gender) AND (users.religion = @religion)
AND (users.status = @status) AND (users.Country = @Country)
AND (users.City = @City)
答案 4 :(得分:0)
请清楚点阿里。
您的数据显示的内容类似于..
userID userName ......... filename
1 ABC c:/phot1.png
1 ABC c:/phot2.png
1 ABC c:/phot2.png
如果我是正确的,那么他们有两种可能的方式来显示您的数据以使您的记录与众不同。
<强>第一强>
userID userName ......... filename
1 ABC c:/phot1.png,c:/phot2.png,c:/phot2.png
这可以通过for xml path('')
OR COALESCE
<强>第二强>
只显示一张照片:
userID userName ......... filename
1 ABC c:/photo1.png
这可以通过应用一些照片条件来实现。