2014-02-09 74 views
1

朋友我卡在一個存儲過程中。我無法顯示不同的記錄。我的名字叫USERSPhotos。兩者都是使用userID的內連接。SQL Server:使用內部聯接和where子句選擇不同記錄

由於表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 

回答

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 
+0

謝謝阿里.... CROSS APPLY邏輯做的工作相當不錯......我從中學到了一些東西。再次感謝朋友:) –

+0

不是問題阿里很高興它幫助:) –

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 

,它會返回唯一結果。

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 
+1

你需要在你的'P'表中包含'photos.filename'列,因爲在主要選擇中引用 – Milen

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) 
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('')COALESCE實現


只顯示一張照片,如:

userID  userName ......... filename 
     1   ABC    c:/photo1.png 

這可能是阿赫通過爲照片應用一些條件來實現。

+0

其祈禱時間我離開網頁,希望這會有道理。 –