表A 表B
a_id userid b_id userid a_id
1 100 1 100 2
2 101 2 150 1
3 101 3 151 2
表A 对表B 为一对多的关系,即表A中的一条记录对应表B中的多条记录,现在求一个sql;查询用户100参与的表A的记录,即要得到表A中的第一、二条记录,
a_id userid
1 100
2 101
表B中外键a_id对应表A的主键a_id;且结果要放在一个结果集中,这个sql乍么写??
a_id userid b_id userid a_id
1 100 1 100 2
2 101 2 150 1
3 101 3 151 2
表A 对表B 为一对多的关系,即表A中的一条记录对应表B中的多条记录,现在求一个sql;查询用户100参与的表A的记录,即要得到表A中的第一、二条记录,
a_id userid
1 100
2 101
表B中外键a_id对应表A的主键a_id;且结果要放在一个结果集中,这个sql乍么写??
from A a
where a.userid = 100
union
select a.a_id,a.userid
from A a,
B b
where b.a_id = a.a_id
and b.userid = 100;
from A a,
(select a_id
from A
where userid = 100
union
select _id
B
where userid = 100) b
where a.a_id =b.a_id
或
select a_id,userid
from A
where a_id in
(select a_id
from A
where userid = 100
union
select _id
B
where userid = 100)