![](/img/trans.png)
[英]mysql php query same table 5 times using the same alias but use that alias for a different column each time
[英]Use Table Alias as Column in MySQL
我有這樣的查詢:
select a,b, (a+b) as c, (a-b) as d, (a+b)*-1 as e, (a+b)/(a-b) as f from test;
有沒有一種方法可以使用我先前定義的別名作為列,因此它將類似於:
(a+b)/(ab) as f from test
到 c/d as f from test
或其他任何我不會在此查詢中重新編碼任何表達式的解決方案。
使用用戶定義的變量 。
SELECT a,b,
@c := (a+b) as c,
@d := (a-b) as d,
@c*-1 as e,
@c/@d as f
FROM test;
select *
from (
select a,b, (a+b) as c, (a-b) as d, (a+b)*-1 as e, (a+b)/(a-b) as f
from test
) s
它們可以有多種方式,但是據我所知,您可以通過使用像這樣的子查詢來實現。 當您將主查詢作為子查詢並根據需要使用它時,您將獲得所有別名作為列。
查看本教程以了解更多內容: http : //www.mysqltutorial.org/mysql-subquery/
select a,b,c,d,e,f form
(select a,b, (a+b) as c, (a-b) as d, (a+b)*-1 as e, (a+b)/(a-b) as f from test)
x;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.