我有一条sql语句
select aid,aa,ab,ac,(select ba from b where bid=aid) as ba, (我还想将ba/aa的结果运算后再输出) as bb from a order by aid asc就是ba是从其他表获取的结果,我想再进行运算
我现在的写法能够实现,就是问是否有必要再写一遍目前的写法:
select aid,aa,ab,ac, (select ba from b where bid=aid) as ba, (select ba from b where bid=aid)/aa as bb from a order by aid asc
这样写对吗,还是有简单办法,因为我总觉得这样sql会去关联两边,效率会低
select aid,aa,ab,ac,(select ba from b where bid=aid) as ba, (我还想将ba/aa的结果运算后再输出) as bb from a order by aid asc就是ba是从其他表获取的结果,我想再进行运算
我现在的写法能够实现,就是问是否有必要再写一遍目前的写法:
select aid,aa,ab,ac, (select ba from b where bid=aid) as ba, (select ba from b where bid=aid)/aa as bb from a order by aid asc
这样写对吗,还是有简单办法,因为我总觉得这样sql会去关联两边,效率会低
select aid,aa,ab,ac, ba, (ba/aa) as bb
from a INNER JOIN b ON bid=aid
order by aid asc
from a left join b on bid=aid order by aid asc
如果我还要用bb进行运算呢
select aid,aa,ab,ac, ba, (ba/aa) as bb (我还想用bb/ab) as bc
from a INNER JOIN b ON bid=aid
order by aid asc
就是as后的字段名怎么再利用?
因为我的sql语句很长,所以才写了一个演示的数据,能理解我说的意思吗
select aid,aa,ab,ac, ba, (ba/aa) as bb,(ba/aa)的其他运算
from a INNER JOIN b ON bid=aid
order by aid asc
from a INNER JOIN b ON bid=aid
order by aid asc
不知道Lz要用于什么场景
刚才举例都是运算,实际是检索,是一个结果集,根据结果集再去第三张表检索数据
select *,bb,bc
from
(
select aid,aa,ab,ac, ba, (ba/aa) as bb ((ba/aa)/ab) as bc--(我还想用bb/ab)
from a INNER JOIN b ON bid=aid
) tb