---==============人员信息表
create table userinfo
(
uid int primary key,
uname varchar(20)not null,
sex varchar(2)not null,
age int not null
)
go
---===========信息表
create table message
(
mid int primary key identity(1,1),--信息编号
sendid int foreign key references userinfo(uid),--发送人
mcontent varchar(2000),--内容
mdate varchar(10),--发送时间
receiveid int foreign key references userinfo(uid)--接收人
)
go查询发送人的姓名,内容,发送时间,接收人姓名 的sql语句

解决方案 »

  1.   

    SELECT u1.uname, m.mcontent, m.mdate, u2.uname
    FROM userinfo u1, userinfo u2, message m
    WHERE m.sendid = u1.uid AND m.receiveid = u2.uid这样不知道行吗,没测试过。
      

  2.   

    看来只能分两步:
    select userinfo.uname,message.* from userinfo,message where userinfo.uid=message.sendid and message.sendid=?;
    select userinfo.uname from userinfo where userinfo.uid=?;
    你message表里同一行的两个字段和userinfo表里不同行的同一字段相关,想要一次查处一行结果应该是不可能的。要么就重新设计表,把message表里的reveiveid改为接收人姓名。
      

  3.   

    做两次连接就可以的(我oracle的)select c.uname as 发送人,c.mcontent as 内容,c.mdate as 时间,d.uname as 接收人 
    from (
    select a.uname ,b.mcontent ,b.mdate  ,b.receiveid 
    from userinfo a,message b 
    where a.userid=b.sendid )c ,userinfo d
    where c.receiveid=d.userid
      

  4.   

    select c.uname as 发送人,c.mcontent as 内容,c.mdate as 时间,d.uname as 接收人 
    from (
    select a.uname ,b.mcontent ,b.mdate  ,b.receiveid 
    from userinfo a,message b 
    where a.userid=b.sendid )c ,userinfo d
    where c.receiveid=d.userid
      

  5.   

    看来只能分两步: 
    select   userinfo.uname,message.*   from   userinfo,message   where   userinfo.uid=message.sendid   and   message.sendid=?; 
    select   userinfo.uname   from   userinfo   where   userinfo.uid=?; 
    你message表里同一行的两个字段和userinfo表里不同行的同一字段相关,想要一次查处一行结果应该是不可能的。要么就重新设计表,把message表里的reveiveid改为接收人姓名。
      

  6.   

    2 楼是对的,之所以是空的是因为你的列值不是noy null,他用的是内连接自然查不出来,用左外连接或有外连接,具体哪个看你怎么连了
      

  7.   

    左连接就行了
    select a.uname,b.mcontent,c.uname 
    from message b left join userinfo a on a.uid=b.sendid
    left join userinfo c on c.uid=b.receiveid