我有我想要顯示像Facebook消息的消息應用程序,它應該由發送者或接收者獲取最後發送的消息,我的表佈局是這樣的:獲取用戶之間的最後一條消息,如Facebook
messages_tbl
__________________________________________________________________________
|id | user1Fk| user2Fk |subject | user1Delete | user2Delete | dateCreated |
和我user_tbl
_______________________________________
| id | first_name | last_name | image |
我查詢
SELECT `id` , `user1Fk` as sender_id,
(SELECT concat(first_name,\" \",last_name)
FROM user_tbl
WHERE user_tbl.id = sender_id
) as senderName,
`user2Fk` as recipient_id ,
(SELECT concat(first_name,\" \",last_name)
FROM user_tbl
WHERE user_tbl.id = recipient_id
) as recipientName,
(SELECT image
FROM user_tbl
WHERE user_tbl.id = sender_id
) as senderImage,
(SELECT image
FROM user_tbl
WHERE user_tbl.id = recipient_id
) as recipientImage,
`subject`, `message`, `user1Delete`, `user2Delete`,
`dateCreated`
FROM `message_tbl`as m1
WHERE dateCreated = (SELECT MAX(m2.dateCreated)
from message_tbl as m2
WHERE (m1.user1Fk = m2.user1Fk
AND m1.user2Fk = m2.user2Fk
OR m1.user1Fk = m2.user2Fk
AND m1.user2Fk = m2.user1Fk
)
) AND ? IN (m1.user1Fk, m1.user2Fk)
ORDER BY dateCreated DESC
此查詢工作的大部分,但其缺乏的,我想它來檢查,如果由?
比賽user1Fk
定id應該再檢查,如果user1Delete
是0或1,如果一個1然後做不顯示消息user 1
刪除它,與用戶2相同,但我想不到的邏輯,任何人都可以幫助我嗎?
類似於https://stackoverflow.com/questions/42740768/mysql-distincs-entry-of-two-columns-even-when-switched/42740903#42740903 – Gab
差不多,但我的已經可以選擇了,我只需要幫助檢查消息是否已被刪除,然後返回 –