求一个多表查询的简单方法....

xiaogueizi 2005-01-13 09:16:19
有两个表table1和table2,两个表通过‘编号’相关联如:
table1
编号 日期 接收人
A1 2004-1-1 aaa
A1 2004-1-1 ccc
A1 2004-1-1 fff
B1 2004-1-2 bbb
C1 2004-1-4 ddd

table2
编号 日期 发送人
A1 2004-1-1 aaa
B1 2004-1-2 bbb
C1 2004-1-4 ddd

比如aaa进入后看到接收人和发送人都是自己的信息该怎么写啊,还要按时间排序?
select * from table1,table2 where table1.编号=table2.编号 and table1.接收人='aaa' or table2.发送人='aaa'
不行列??
...全文
119 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
long_205 2005-01-13
  • 打赏
  • 举报
回复
select a.*,b.*
from table1 a inner join table2 b
on a.编号 = b.编号
where a.接收='aaa' and b.发送 = 'aaa'
order by b.日期

xiaogueizi 2005-01-13
  • 打赏
  • 举报
回复
就是说接收人是aaa"或者"说发送人是aaa的....不能出现相同编号的..
xiaogueizi 2005-01-13
  • 打赏
  • 举报
回复
正确的记录集应该是
A1 2004-1-1 aaa
daijingjie2002 2005-01-13
  • 打赏
  • 举报
回复
select * from table1 inner join table2 on table1.编号=table2.编号 where table1.接收人
='aaa' and table2.发送人='aaa'
xiaogueizi 2005-01-13
  • 打赏
  • 举报
回复
不好意思,是我说掉了,同一个编号不能出现两次!!!这样写A1就出现了两次啊
zjcxc 元老 2005-01-13
  • 打赏
  • 举报
回复
select a.*,b.*
from table1 a inner join table2 b
on a.编号 = b.编号
where a.接收='aaa' and b.发送 = 'aaa'
order by b.日期
子陌红尘 2005-01-13
  • 打赏
  • 举报
回复
select a.*,b.*
from table1 a inner join table2 b
on a.编号 = b.编号
where b.发送 = 'aaa'
order by b.日期
xiaogueizi 2005-01-13
  • 打赏
  • 举报
回复
而且用了group by就不能排序了

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧