表a有字段id,p1,p2,p3,p4.......,表b有字段id,m
a表数据, b表数据
111, 1,2,3,4........ 1,红
2,黄
3,蓝
4,白
a.p1=b.m,a.p2=b.m
想查询出
111,红,2,黄,3,蓝,4,白 等方式语句如何写
111,红,2,黄,3,蓝,4,白 等方式语句如何写
select a.id,(select m from b where b.id=a.p1),a.p1,(select m from b where b.id=a.p2),a.p2,(select m from b where b.id=a.p3),a.p3,(select m from b where b.id=a.p4),a.p4 from a
select a.id,b.id,b.m from a ,b where a.id = '111';
基本结构就是查询你要查的数据+from表+ where条件
试一下设置关联和主键
select a.id,a.p1,b.m,a.p2,c.m,a.p3,d.m,a.p4,e.m
from a表 a
left join b表 b
on a.p1=b.m
left join b表 c
on a.p2=c.m
left join b表 d
on a.p3=d.m
left join b表 e
on a.p4=e.m
where a.id='111';
select a.id,a.p1,b.m,a.p2,c.m,a.p3,d.m,a.p4,e.m
from a表 a
left join b表 b
on a.p1=b.id
left join b表 c
on a.p2=c.id
left join b表 d
on a.p3=d.id
left join b表 e
on a.p4=e.id
where a.id='111';
111,红,2,黄,3,蓝,4,白 等方式语句如何写
select a.id,(select m from b where b.id=a.p1),a.p1,(select m from b where b.id=a.p2),a.p2,(select m from b where b.id=a.p3),a.p3,(select m from b where b.id=a.p4),a.p4 from a