Oracle ALIAS

 執行以下query


SELECT ENAME , SAL , COMM

FROM EMP;

ENAMESALCOMM
KING5000-
BLAKE2850-
CLARK2450-
JONES2975-
SCOTT3000-
FORD3000-
SMITH800-
ALLEN1600300
WARD1250500
MARTIN12501400


但看ENAME , SAL , COMM 不夠直觀,
可以改成以下query:

SELECT ENAME EMPLOYEENAME, SAL SALARY, COMM COMMISSION
FROM EMP;

EMPLOYEENAMESALARYCOMMISSION
KING5000-
BLAKE2850-
CLARK2450-
JONES2975-
SCOTT3000-
FORD3000-
SMITH800-
ALLEN1600300
WARD1250500
MARTIN12501400

這樣看欄位名稱比較容易看懂

另外這樣子的寫法中間不允許空格
SELECT ENAME EMPLOYEE NAME, SAL SALARY, COMM COMMISSION
FROM EMP;
Oracle會直接報錯誤


如果想要讓欄位呈現中間是空格的話要修改成這樣:
SELECT ENAME "EMPLOYEE NAME", SAL SALARY, COMM COMMISSION
FROM EMP;

EMPLOYEE NAMESALARYCOMMISSION
KING5000-
BLAKE2850-
CLARK2450-
JONES2975-
SCOTT3000-
FORD3000-
SMITH800-
ALLEN1600300
WARD1250500
MARTIN12501400

這樣就看的到差異了

此外也可以使用AS

SELECT 
ENAME AS "EMPLOYEE NAME", 
SAL AS SALARY, 
COMM COMMISSION
FROM EMP;

會得到一樣的結果
EMPLOYEE NAMESALARYCOMMISSION



也可以加入字串
如以下query
SELECT 'hello my name is ' || ename as "concatenated value"
FROM EMP
WHERE job = 'MANAGER'

concatenated value
hello my name is BLAKE
hello my name is CLARK
hello my name is JONES


修改一下query
SELECT  'Hello, I am ' || ename || ' , I make $' || sal || ' per month' as "employee income"
FROM EMP

employee income
Hello, I am KING , I make $5000 per month
Hello, I am BLAKE , I make $2850 per month
Hello, I am CLARK , I make $2450 per month
Hello, I am JONES , I make $2975 per month
Hello, I am SCOTT , I make $3000 per month
Hello, I am FORD , I make $3000 per month
Hello, I am SMITH , I make $800 per month
Hello, I am ALLEN , I make $1600 per month
Hello, I am WARD , I make $1250 per month
Hello, I am MARTIN , I make $1250 per month

0 Comments:

張貼留言