Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 一次读取与结果集中的每条记录相关的多条记录_Sql Server_Union_Sap Ase_Sql Cte - Fatal编程技术网

Sql server 一次读取与结果集中的每条记录相关的多条记录

Sql server 一次读取与结果集中的每条记录相关的多条记录,sql-server,union,sap-ase,sql-cte,Sql Server,Union,Sap Ase,Sql Cte,我有三张桌子: 消息表包含MsgID,MsgText,MsgDate列MsgId是主键 MessageSender表包含MsgId,SenderId,SenderNameMsgId是FK MessageTo表包含MsgId,ToId,ToNameMsgId是FK 每封邮件将有一个发件人。但是它可以有很多接收者。i、 e.对于消息表中的每条记录,在消息发送者表中有一条记录,在消息收件人表中有多条记录 我想在一个查询中或一次获取所有消息的所有详细信息。e、 g.对于特定邮件,谁是发件人,谁都是收

我有三张桌子:

  • 消息
    包含
    MsgID
    MsgText
    MsgDate
    MsgId
    是主键
  • MessageSender
    表包含
    MsgId
    SenderId
    SenderName
    MsgId
    是FK
  • MessageTo
    表包含
    MsgId
    ToId
    ToName
    MsgId
    是FK
每封邮件将有一个发件人。但是它可以有很多接收者。i、 e.对于
消息
表中的每条记录,在
消息发送者
表中有一条记录,在
消息收件人
表中有多条记录

我想在一个查询中或一次获取所有消息的所有详细信息。e、 g.对于特定邮件,谁是发件人,谁都是收件人

我该怎么做

我使用的是MSSQL Server 2005和Sybase 15

注:我在此仅给出了表格的相关细节。我不能更改表模式,因为它已经在生产环境中存在很长时间了

select distinct M.MsgID, M.MsgText, MS.SenderID, MS.SenderName, 
MT.ToName, MT.ToId 
from Message M, MessageSender MS, MessageTo MT
where M.MessageID = MS.MessageID and 
M.MessageID = MT.MessageID 
and MsgDate like '%20130307%' -- To get the messages sent on 07-Mar-2013