你这是个典型的横表转竖表的问题。
1、首先将b表结构转成与a表结构一致:
select * from(
select bc, '1h' as y, 1h as h from b
union
select bc, '2h' as y, 2h as h from b
union
select bc, '3h' as y, 3h as h from b
union
select bc, '4h' as y, 4h as h from b
union
select bc, '5h' as y, 5h as h from b
union
select bc, '6h' as y, 6h as h from b
union
select bc, '7h' as y, 7h as h from b
union
select bc, '8h' as y, 8h as h from b
union
select bc, '9h' as y, 9h as h from b
union
select bc, '10h' as y, 10h as h from b
union
select bc, '11h' as y, 11h as h from b
union
select bc, '12h' as y, 12h as h from b
)tb
order by bc, y
上面的tb临时表就跟a表结构一致了。
2、tb表与a表做简单的关联即可:
select * from a, (
select bc, '1h' as y, 1h as h from b
union
select bc, '2h' as y, 2h as h from b
union
select bc, '3h' as y, 3h as h from b
union
select bc, '4h' as y, 4h as h from b
union
select bc, '5h' as y, 5h as h from b
union
select bc, '6h' as y, 6h as h from b
union
select bc, '7h' as y, 7h as h from b
union
select bc, '8h' as y, 8h as h from b
union
select bc, '9h' as y, 9h as h from b
union
select bc, '10h' as y, 10h as h from b
union
select bc, '11h' as y, 11h as h from b
union
select bc, '12h' as y, 12h as h from b
)tb
where a.c = tb.c and a.y = tb.y
上面SQL结果就可以直观的看到你要的数据了。
追问我能加一下你的qq吗!在这边一下字数就满了
追答私信你了