0

我有这个查询

select count(distinct user1_.Id) as col_0_0_ from [FriendCloseness] friendclos0_ inner join [User] user1_ on friendclos0_.User_id=user1_.Id where dateadd(d, 0, datediff(d, 0, friendclos0_.LastUpdate))=@p0 and friendclos0_.IsInvited=1 and user1_.IsDeleted=0 and user1_.Sex=@p1 

表用户加入 FriendCloseness 查询 FriendClossness 字段 LastUpdate、IsInvited、用户字段 IsDeleted、Sex where

涉及查询的表用户结构

User Id,(Primary key) IsDeleted, Sex,

FriendClossness Id,(主键,DBMS会自动设置索引为主键吗?) LastUpdate (datetimeoffset), IsInvited, User_id (外键)

我知道我需要索引哪个字段,我想通过创建新字段“InviteDate”来删除日期函数以仅存储日期

4

1 回答 1

1

试试这个——

SELECT col_0_0_ = COUNT(DISTINCT u.Id)
FROM (
    SELECT u.Id
    FROM dbo.[User] u
    WHERE u.IsDeleted = 0 
        AND u.Sex = @p1
    --GROUP BY u.Id
) u
JOIN (
    SELECT 
          f.[user_id]
        , LastUpdate = DATEADD(DAY, 0, DATEDIFF(DAY, 0, f.LastUpdate))
    FROM dbo.[FriendCloseness] f
    WHERE f.IsInvited = 1 
) f ON f.[user_id] = u.Id
WHERE f.LastUpdate = @p0 
于 2013-04-24T09:57:10.360 回答