2011-12-30 87 views
2

我正在开发一个信息系统,并且我已经和消息中心。我想显示诸如Outlook或Gmail“对话”之类的消息。SQL Server显示消息如对话

在我的消息表,我有以下栏目:

  • 的MessageId(PK)
  • 日期
  • 从(FK)
  • 要(FK)
  • 身体
  • 主题
  • 主题
  • ReplyId(FK),空

首先我得到没有replyId,以显示 “最新” 消息的消息。

但我不明白的谈话,当消息的任何点击... :(

任何一个可以帮助我查询提前

感谢

编辑? 我添加一个额外的农田到餐桌,最后回复到对话的ID,这样我就可以在同一时间得到所有会话消息!

:)

+0

1.你能告诉您使用的是当前查询? 2.答案可以是在谈话中间的消息,还是仅在谈话开始时?即,如果您搜索replyId = messageId,您是否会始终获得所有答复。 – mellamokb 2011-12-30 15:00:25

+0

我的查询:从消息为m 内加入消息作为答复上m.MessageId = reply.ReplyId 其中m.MessageId = @ messageId' 我 '声明@messageId INT = 4 选择答复* 。只能得到对messageId = 4的回复...但是,messageId = 1是回复messageId = 2,messageId = 2回复到messageId = 3,依此类推... 谢谢! – joaoasrosa 2011-12-30 15:07:40

+1

通常情况下,回复消息中最好保留回复的消息ID,而不是其他方式 - 当前模型允许两条消息接收相同的回复消息(有点不寻常),并且不会有消息不止一次地回复 - 在这样的模型中,对话中的原始消息具有空值,然后从那里搜索回复。让对话中的每条信息*也参考原始信息可以改善事情。 – 2011-12-30 16:13:10

回答

1

你想用一个CTE做到这一点,它本质上是一个亲子查询:

declare @MessageId int = 3; 

with Conversations as 
(
    select 
     MessageId, 
     [Date], 
     [From], 
     [To], 
     Body, 
     [Subject], 
     [State], 
     ReplyId, 
     CAST(MessageId as varchar(max)) + '-' as hierarchy, 
     CAST(MessageId as varchar(max)) + '-' as TopMessageId 
    from 
     Messages 
    where 
     replyid is null 
    union all 
    select 
     m.MessageId, 
     m.[Date], 
     m.[From], 
     m.[To], 
     m.Body, 
     m.[Subject], 
     m.[State], 
     m.ReplyId, 
     c.hierarchy + CAST(m.MessageId as varchar(max)) + '-' as hierarchy, 
     c.TopMessageId 
    from 
     Conversations c 
     inner join Messages m on 
      c.MessageId = m.ReplyId 
) 

select 
    c2.MessageId, 
    c2.[Date], 
    c2.[From], 
    c2.[To], 
    c2.Body, 
    c2.[Subject], 
    c2.[State], 
    c2.ReplyId 
from 
    Conversations c1 
    inner join Conversations c2 on 
     c2.hierarchy like c1.TopMessageId + '%' 
where 
    c1.MessageId = @MessageId 
order by c2.[Date] desc 

如果您使用SQL Server 2008中,你可能要考虑使用你的桌子上一个hierarchyid列一起避免CTE。

+0

感谢您的回复。 我正在使用SQL Server 2008,但我从来没有使用'hierarchyid' ...我要阅读文档。 :) – joaoasrosa 2011-12-30 16:32:20

0

我想你正在寻找的是使用sql server进行递归,因为你希望你有对话作为父子关系。

0

我写这篇文章,它是一个简单的聊天转换:

session_start(); 
$regreg = $_SESSION['username']; 
$fgetrhge = $_GET['with']; 

$pdo = new PDO('mysql:host=localhost;dbname=TESTDB', 'TESTUSER', 'TESTPASS'); 

$sql = "SELECT an, von, message, time FROM messages ORDER BY time ASC"; 

foreach ($pdo->query($sql) as $row) { 

if($_SESSION['username'] == $row['von'] && $_GET['with'] == $row['an']) { 
echo '<div align="right"> 
<div class="own"> 

<span style="margin-right:18px;" class="nmespn">DU</span> 
<img src="http://YOURDOMAIN.NET/profileicons/'.$regreg.'" class="pbmspna" onerror="this.onerror=null;this.src='."'http://crashhd.de/no_pic.png'".';"> 
<div style="float:left;padding-top:2px;" align="left"> 
<span class="tmespn">'.$row['time'].'</span> 
</div> 
<br><br> 
<div align="left" style="margin-left:14px" class="msgspn"> 
<span>'.$row['message'].'</span> 
</div> 

</div> 
</div> 
<br>'; 
} 
else if($_GET['with'] == $row['von'] && $_SESSION['username'] == $row['an']) { 

echo '<div align="left"> 
<div align="left" class="partner"> 
<img src="http://YOURDOMAIN.NET/profileicons/'.$row['von'].'" class="pbmspna" onerror="this.onerror=null;this.src='."'http://crashhd.de/no_pic.png'".';"> 
<span class="nmespn" style="cursor:pointer;">'.$row['von'].'</span> 
<div style="float:right;padding-right:6px;" align="right"> 
<span class="tmespn">'.$row['time'].'</span> 
</div> 
<br><br> 
<div class="msgspn"> 
<span>'.$row['message'].'</span> 
</div> 
</div> 
</div> 
<br>'; 
    } 
} 

// an = to; von = from; sry im german :D 

我希望我能帮助:)