我有以下表格:
UserConversation
:userId和ConversationId ConversationMessageReceipt
:userId,conversationId,conversationMessageId和其他列ConversationMessageReceipt
:conversationId,conversationMessageId,userid 我可以添加更多细节,但我基本上想在一个表中找到也存在于另一个表中的行,即从第一个表中找到的所有行必须存在于另一个表中。
有没有办法找到第二个表中的所有行或获得空结果?
由于
答案 0 :(得分:1)
尝试加入以下内容:
SELECT *
FROM UserConversation uc INNER JOIN ConvesationMessageReceipt cmr
ON uc.userId = cmr.userId
AND uc.conversationId = cmr.conversationId
答案 1 :(得分:1)
使用INNER JOIN你的场景,
SELECT * FROM UserConversation INNER JOIN ConvesationMessageReceipt
ON UserConversation.userId = ConvesationMessageReceipt.userId
AND UserConversation.conversationId = ConvesationMessageReceipt.conversationId
或者你想从第一张表中获取所有记录使用LEFT JOIN或LEFT OUTER JOIN
SELECT * FROM UserConversation LEFT JOIN ConvesationMessageReceipt
ON UserConversation.userId = ConvesationMessageReceipt.userId
AND UserConversation.conversationId = ConvesationMessageReceipt.conversationId
答案 2 :(得分:1)
如果要检查表2中的table1数据,还可以使用EXISTS关键字..
SELECT *
FROM UserConversation uc
WHERE EXISTS (SELECT 1
FROM ConversationMessageReceipt cm
WHERE uc.userId=cm.userid
AND uc.conversationid=cm.conversationid)