[英]How to format query result in Oracle?
I have a table called EMP which has a column called JOB. 我有一个名为EMP的表,该表具有一个称为JOB的列。 Now, JOB column has various entries like CLERK, SALESMAN, MANAGER, etc. I want to fetch all rows(&columns) where JOB = 'CLERK' but instead of displaying 'CLERK' I want to display 'NEW CLERK'.
现在,JOB列具有各种条目,如CLERK,SALESMAN,MANAGER等。我想获取所有的行(和列),其中JOB ='CLERK',但我不想显示'CLERK',而是要显示'NEW CLERK'。 I am not supposed to change the value in the table but display it different while querying.
我不应该更改表中的值,但在查询时将其显示为不同的值。 Is this possible?
这可能吗?
[Sorry if it doesn't make sense - it's a homework question] [抱歉,如果这没有意义-这是一个作业问题]
One way to do this is with a CASE expression, for example: 一种实现方法是使用CASE表达式,例如:
SELECT CASE WHEN t.job = 'CLERK' THEN 'NEW CLERK' ELSE t.job END AS job
, t.other_col
FROM emp t
This will return the value from the JOB
column, except replace occurrences of 'CLERK'
with the literal value of 'NEW CLERK'
. 这将从
JOB
列返回值,除了将'CLERK'
替换为文字值'NEW CLERK'
。
If you have several different replacements to do, the search form of the CASE expression can be handy: 如果您要进行几种不同的替换,则CASE表达式的搜索形式会很方便:
SELECT CASE t.job
WHEN 'CLERK' THEN 'NEW CLERK'
WHEN 'SALESMAN' THEN 'GOOD SALESMAN'
ELSE t.job
END AS job
, t.other_col
FROM emp t
The 'search' form is just a shorter way of writing: “搜索”形式只是一种较短的书写方式:
SELECT CASE
WHEN t.job = 'CLERK' THEN 'NEW CLERK'
WHEN t.job = 'SALESMAN' THEN 'GOOD SALESMAN'
ELSE t.job
END AS job
, t.other_col
FROM emp t
If you are only returning rows that have 'CLERK' in the JOB column, then you could simply return a literal in the statement: 如果仅返回在JOB列中具有“ CLERK”的行,则只需在语句中返回一个文字:
SELECT 'NEW CLERK' AS job
, t.other_col
FROM emp t
WHERE t.job = 'CLERK'
--or-- - 要么 -
SELECT 'NEW '||t.job AS job
, t.other_col
FROM emp t
WHERE t.job = 'CLERK'
It is possible. 有可能的。 Something like this should work:
这样的事情应该起作用:
select case JOB when 'CLERK' then 'NEW CLERK' else JOB end as JOB from your_table
You can use case statement 您可以使用案例陈述
SELECT empno,
ename,
CASE WHEN job = 'CLERK' THEN 'NEW CLERK' ELSE job END job,
mgr,
hiredate,
sal,
comm,
deptno
FROM emp
WHERE job = 'CLERK'
You can try: 你可以试试:
SELECT *,
CASE WHEN JOB = 'CLERK'
THEN 'NEW CLERK'
END
FROM EMP
if you already filter by job, you can display the new value directly as a constant. 如果您已经按工作过滤,则可以将新值直接显示为常量。
SELECT 'NEW CLERK' as job
FROM EMP
WHERE job='CLERK'
you also can use decode
instead of CASE
: 您还可以使用
decode
而不是CASE
:
SELECT decode(job,'CLERK','NEW CLERK',job)
FROM EMP
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.