具体详细问题是这样的:
表table1 A(int id,varchar name...),table2 B(int id,varchar title...)
task任务是需要写一个sql语句
select a.id as id,a.name as name,
b.title as sender,[b.id=某个条件1]
b.title as recever,[b.id=某个条件2]
from A a,B b
where a.id=#id#;
也就是说B表的title取出来是作为两个名词的。
不知道什么思路去实现呢?
谢谢各位的解答.
7 个解决方案
#1
select a.id as id,a.name as name,
b.title as sender,
b.title as recever
from A a join B b on a.id = b.aid
join B c on a.pid = b.aid
#2
left join
#3
--1 id 多的话
select a.id as id,a.name as name,
b.title as sender, (select b.id from B where 某个条件1)
b.title as recever,(select b.id from B where 某个条件2)
from A a,B b
where a.id=#id#;
--2 id 少
select a.id as id,a.name as name,
b.title as sender, case when 某个条件1 then ** else *** end
b.title as recever,case when 某个条件2 then ** else *** end
from A a,B b
where a.id=#id#;
#4
select a.id as id,a.name as name,
b.title as sender,[b.id=某个条件1]
b.title as recever,[b.id=某个条件2]
from A a,B b,B c
where a.id=#id# and a.id=b.id and a.id=c.id
#5
OrchidCat 的 不错
#6
感谢您的帮助。问题解决了。不过您上面select的时候应该写成这样的:
select a.id as id,a.name as name,
b.title as sender,
c.title as recever
from A a join B b on a.id = b.aid
join B c on a.pid = b.aid
select a.id as id,a.name as name,
b.title as sender,
c.title as recever
from A a join B b on a.id = b.aid
join B c on a.pid = b.aid
#7
结贴感谢各位给予思路解答的人员。
#1
select a.id as id,a.name as name,
b.title as sender,
b.title as recever
from A a join B b on a.id = b.aid
join B c on a.pid = b.aid
#2
left join
#3
--1 id 多的话
select a.id as id,a.name as name,
b.title as sender, (select b.id from B where 某个条件1)
b.title as recever,(select b.id from B where 某个条件2)
from A a,B b
where a.id=#id#;
--2 id 少
select a.id as id,a.name as name,
b.title as sender, case when 某个条件1 then ** else *** end
b.title as recever,case when 某个条件2 then ** else *** end
from A a,B b
where a.id=#id#;
#4
select a.id as id,a.name as name,
b.title as sender,[b.id=某个条件1]
b.title as recever,[b.id=某个条件2]
from A a,B b,B c
where a.id=#id# and a.id=b.id and a.id=c.id
#5
OrchidCat 的 不错
#6
感谢您的帮助。问题解决了。不过您上面select的时候应该写成这样的:
select a.id as id,a.name as name,
b.title as sender,
c.title as recever
from A a join B b on a.id = b.aid
join B c on a.pid = b.aid
select a.id as id,a.name as name,
b.title as sender,
c.title as recever
from A a join B b on a.id = b.aid
join B c on a.pid = b.aid
#7
结贴感谢各位给予思路解答的人员。