简体   繁体   中英

Tricky SQL select

I have a message table, this table contains the following columns :

id | fromUserId | toUserId | sentDate | readDate | message | subject | fromUserDeleted(bit) | toUserDeleted(bit)

Now I need to select first message within every conversionen no mather if its from or to the current user. What makes it more complex is that I need to exclude messages that are deleted by the current user, so if the message is from current user then the fromUserDeleted have to be 0 if the message is from another user then toUserDeleted needs to be 0.

I know that there already are a lot of examples on how to select first post within groups but this is a bit more complex. The current user might be in toUserId or in fromUserId and the same goes for the deleted column.

This is what I have tried :

SELECT 
    m1.*, 
    (SELECT count(*) FROM mail m3 WHERE m3.fromUserId=m1.fromUserId AND m3.toUserId=m1.toUserId AND m3.toUserDeleted = 0) as messageCount, 
    (SELECT count(*) FROM mail m4 WHERE m4.toUserId=15 AND m4.readDate is null AND m4.toUserDeleted=0) as messagesNotRead 
FROM 
    mail m1 
LEFT JOIN mail m2 ON 
    ((m1.fromUserId = m2.fromUserId) and
    m2.)
WHERE 
    m2.id IS NULL AND 
    (m1.toUserId = 15 OR m1.fromUserId = 15) 
ORDER BY 
    m1.sentDate DESC;

And :

SELECT 
    * 
FROM 
    mail m1
WHERE
    ((m1.fromUserId = 15 AND m1.fromUserDeleted=0) OR (m1.toUserId = 15 AND m1.toUserDeleted=0)) AND
    m1.id = (SELECT m2.id FROM mail m2 WHERE m2.fromUserId = 15 OR m2.toUserId = 15 OR m2.fromUserId = m1.fromUserId OR m2.fromUserId = m1.toUserId OR m2.toUserId = m1.fromUserId OR m2.toUserId = m1.toUserId order By m2.sentDate desc limit 0,1)
Order by m1.sentDate DESC

None of these will do what I need. Pleas help!

It´s okay if a stored procedure needs to be created.

IMPORTANT : This i MySQL

EDIT 1:

Pleas see this for example : http://sqlfiddle.com/#!2/6a2ef/3

Thanks for the feedback. Hopefully this will address your need:

DECLARE @UserID INT
SELECT @UserID = 15

SELECT *
FROM (
    SELECT UserID, ConversationWithUserID, sentDate, readDate, [message], [subject], ROW_NUMBER() OVER (PARTITION BY ConversationWithUserID ORDER BY sentDate ASC) AS MessageOrder
    FROM (
        SELECT id, fromUserId AS UserID, toUserId AS ConversationWithUserID, sentDate, readDate, [message], [subject]
        FROM dbo.mail
        WHERE fromUserID = @UserID AND fromUserDeleted = 0
        UNION
        SELECT id, toUserId AS UserID, fromUserId AS ConversationWithUserID, sentDate, readDate, [message], [subject]
        FROM dbo.mail
        WHERE toUserId = @UserID AND toUserDeleted = 0
    ) x
) y
WHERE MessageOrder = 1
ORDER BY y.UserID, y.ConversationWithUserID

DOING UNPIVOT to get fromUSerId, toUserId under same column so that we can pick correct userId based on the sentDate.

SELECT T.*

FROM
(
    SELECT *,

           ROW_NUMBER() OVER ( PARTITION BY userVal ORDER BY sentDate DESC) as seq
    FROM
    (SELECT 
            *

     FROM msg
     WHERE (fromUserId = @userID AND fromUserDeleted = 0 ) 
           OR  (toUserId = @UserID AND toUserDeleted = 0)

    )p
    unpivot
    ( userVal for UserId in ( fromUserId, toUserId)
    )as unpvt
) T
WHERE seq =1 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM