topic(tid, subject) -- 有二条, 记录
post(pid, tid, contents) -- 有十条
home(hid, tid, pid, hevent, horder) -- 有五条
现在要以home表的字段作为条件分别查询topic 和post 二表,
得出hid, subject, contents, 列, 怎么查 ?
select h.hid, h.hevent, t.subject from home as h , topic as t where t.tid = h.tid -- 查出二条
select p.contents from home as h , post as p where p.pid = h.pid -- 查出四条
select h.hid, t.subjest from home as h , topic as t where t.tid = h.tid
select h.hid, p.contents from home as h , post as p where p.pid = h.pid
post(pid, tid, contents) -- 有十条
home(hid, tid, pid, hevent, horder) -- 有五条
现在要以home表的字段作为条件分别查询topic 和post 二表,
得出hid, subject, contents, 列, 怎么查 ?
select h.hid, h.hevent, t.subject from home as h , topic as t where t.tid = h.tid -- 查出二条
select p.contents from home as h , post as p where p.pid = h.pid -- 查出四条
select h.hid, t.subjest from home as h , topic as t where t.tid = h.tid
select h.hid, p.contents from home as h , post as p where p.pid = h.pid
from home,topic,post
where home.tid=topic.tid and home.pid=post.pid
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
select *
from home a left join topic b on a.tid=b.tid
left join post c on a.pid=c.pid
就是这样, 开始我用left join 把on条件弄错, 结果得不到答案,
表的结构复杂,数据多就不帖了, 大概意思就是把不同结构的表的多个查询结果连续在一起。