[英]Reuse variables in sql query
I want to know is there a way to reuse variables defined as AS
and not to rewrite our code every time or not?我想知道有没有办法重用定义为
AS
变量而不是每次都重写我们的代码?
Example
I have such line:我有这样的一行:
IFNULL((CASE WHEN (((c.gaji_pokok+c.tunjangan+q.jkk+q.jkm)*12)+('c.komisi' * 'c.thr') >= 6000000) then ((c.gaji_pokok+c.tunjangan+q.jkk+q.jkm)*12)+('c.komisi' * 'c.thr')*0.05 ELSE 6000000 end),0) as b_jabatan,
and the next time I need such command I just want to use b_jabatan
instead of writing whole line下次我需要这样的命令时,我只想使用
b_jabatan
而不是写整行
example
IFNULL((b_jabatan,0) as brutto_tahun,
is that possible?那可能吗?
Use the least()
function:使用
least()
函数:
coalesce(least( ((c.gaji_pokok+c.tunjangan+q.jkk+q.jkm)*12)+(c.komisi * c.thr)*0.05, 6000000
), 0
) as b_jabatan,
You can't reuse an alias in the same scope - left apart the order by
clause (and, in MySQL, the group by
and having
clauses).您不能在同一范围内重复使用别名 - 将
order by
子句(以及,在 MySQL 中, group by
和having
子句)分开。
You either need to repeat the expression, or use a subquery or CTE (which creates a new scode).您要么需要重复表达式,要么使用子查询或 CTE(创建新的 scode)。 So:
所以:
select t.*, coalesce(b_jabatan,0) as brutto_tahun, ...
from (
select
< your super long expression > as b_jabatan,
...
from ...
) t
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.