-2

我有一个名为的表CharUser有以下数据

UserID UserName UserPhone UserAddress

还有一张桌子叫subscriptionjob

subID userId   companyid

另一个名为 user 的表UsersCvs有以下数据

CVId   Companyuserid   cvpath

我想做一个查询,每行的结果如下

userid   username   userphone  useraddress usercv

用户 ID 是从subcriptionjob表中给出的,我尝试了以下第一个查询

SELECT        CharUserId, CharUserName, CharUserPassword, CharUserMail, CharUserPhone, CharUserAddress, CharUserGender, ProgId, CharUserBirthdate
FROM            CharUser
WHERE        (CharUserId IN
                             (SELECT        CompanyUserId
                                FROM            SubsciptionJob
                                WHERE        (JobId = 1)))

这是第二个查询

SELECT        TOP (1) CvPath
FROM            UsersCvs
WHERE        (CompanyUserId IN
                             (SELECT        CompanyUserId
                                FROM            SubsciptionJob AS SubsciptionJob_1
                                WHERE        (JobId = 1)))
ORDER BY CvId DESC

两者都可以单独工作,我尝试将它们结合起来,但它不起作用

我想将 cvpath 列附加到第一个查询,每行都有一个唯一值

有任何想法吗.. ?

谢谢你

4

3 回答 3

0

这比上面的答案更有效:

SELECT CharUserId, 
       CharUserName, 
       CharUserPassword, 
       CharUserMail, 
       CharUserPhone, 
       CharUserAddress, 
       CharUserGender, 
       ProgId, 
       CharUserBirthdate,
       (SELECT TOP (1) CvPath FROM UsersCvs c where c.CompanyUserID=X.CharUserID
       FROM CharUser X
       WHERE (CharUserId IN 
                           (SELECT CompanyUserId FROM SubsciptionJob 
                            WHERE (JobId = 1)))

通过在订阅表和 CharUser 表之间使用内部连接可能会更有效。

于 2012-05-19T09:58:02.557 回答
0

利用ROW_NUMBER()

SELECT CharUserId, CharUserName, CharUserPassword, CharUserMail, 
    CharUserPhone, CharUserAddress, 
    CharUserGender, ProgId, CharUserBirthdate,cvpath
FROM CharUser CU
JOIN subscriptionjob SJ ON SJ.UserId = CU.UserID
INNER JOIN 
(   SELECT cvpath,Companyuserid,
    ROW_NUMBER() OVER (PARTITION BY A.Companyuserid ORDER BY A.CVID DESC) X
    FROM UsersCvs A
)R ON R.X = 1 AND CU.UserID = R.Companyuserid
WHERE JobId = 1
于 2012-06-15T23:19:57.213 回答
0

一种方法:

SELECT CharUserId
     , CharUserName
     , CharUserPassword
     , CharUserMail
     , CharUserPhone
     , CharUserAddress
     , CharUserGender
     , ProgId
     , CharUserBirthdate
     , (SELECT TOP (1) CvPath
       FROM UsersCvs
       WHERE CompanyUserId IN (SELECT CompanyUserId
                               FROM SubsciptionJob
                               WHERE JobId = 1)
       ORDER BY CvId DESC) AS CvPath    
FROM CharUser
WHERE CharUserId IN (SELECT CompanyUserId
                     FROM  SubsciptionJob
                     WHERE JobId = 1)
于 2012-05-19T09:40:01.013 回答