暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 帮助学生学习SQL的指导

ASKTOM 2019-08-08
354

问题描述

代码有什么问题,为什么脚本不会运行?
<代码
选择员工身份 | | 名字 | | 最后名称 | |
'加入公司' | | hire_date | | '和' 都'
TRUNC(SYSDATE-hire_date/31) | | '多年经验'
来自员工;
代码>
提前谢谢,
多萝西。

专家解答

好吧,让我们一次完成

SQL> SELECT employee_id || first_name || last_name ||
  2  'joined the company in' || hire_date || 'and has'
  3  TRUNC(SYSDATE-hire_date/31) || 'years experience'
  4  FROM hr.employees;
TRUNC(SYSDATE-hire_date/31) || 'years experience'
     *
ERROR at line 3:
ORA-00923: FROM keyword not found where expected


在第3行,我期望从,即,选择的结尾,因此我必须在第2行缺少一些东西,即,我想继续连接。所以我加上 | |

SQL>
SQL>
SQL> SELECT employee_id || first_name || last_name ||
  2  'joined the company in' || hire_date || 'and has' ||
  3  TRUNC(SYSDATE-hire_date/31) || 'years experience'
  4  FROM hr.employees;
TRUNC(SYSDATE-hire_date/31) || 'years experience'
              *
ERROR at line 3:
ORA-00932: inconsistent datatypes: expected NUMBER got DATE


这是一个括号内的问题。我正在尝试进行hiredate/31 (即日期除以数字),这是非法的。我需要先减法,然后除法。所以我加了括号

SQL>
SQL> SELECT employee_id || first_name || last_name ||
  2  'joined the company in' || hire_date || 'and has' ||
  3  TRUNC((SYSDATE-hire_date)/31) || 'years experience'
  4  FROM hr.employees;

EMPLOYEE_ID||FIRST_NAME||LAST_NAME||'JOINEDTHECOMPANYIN'||HIRE_DATE||'ANDHAS'||TRUNC((SYSDATE-HIRE_DATE)/31)||'YEARSEXPERIENCE'
-------------------------------------------------------------------------------------------------------------------------------------------
100StevenKingjoined the company in17-JUN-03and has190years experience
101NeenaKochharjoined the company in21-SEP-05and has163years experience
102LexDe Haanjoined the company in13-JAN-01and has218years experience
103AlexanderHunoldjoined the company in03-JAN-06and has160years experience
104BruceErnstjoined the company in21-MAY-07and has143years experience
105DavidAustinjoined the company in25-JUN-05and has166years experience
106ValliPataballajoined the company in05-FEB-06and has159years experience
107DianaLorentzjoined the company in07-FEB-07and has147years experience
108NancyGreenbergjoined the company in17-AUG-02and has200years experience
109DanielFavietjoined the company in16-AUG-02and has200years experience
110JohnChenjoined the company in28-SEP-05and has163years experience
111IsmaelSciarrajoined the company in30-SEP-05and has163years experience
112Jose ManuelUrmanjoined the company in07-MAR-06and has158years experience
113LuisPoppjoined the company in07-DEC-07and has137years experience
...
...

107 rows selected.


好的,现在可以了。我正在添加一个 “txt” 别名,只是为了一个更好的标题

SQL>
SQL> SELECT employee_id || first_name || last_name ||
  2  'joined the company in' || hire_date || 'and has' ||
  3  TRUNC((SYSDATE-hire_date)/31) || 'years experience' txt
  4  FROM hr.employees;

TXT
-------------------------------------------------------------------------------------------------------------------------------------------
100StevenKingjoined the company in17-JUN-03and has190years experience
101NeenaKochharjoined the company in21-SEP-05and has163years experience
102LexDe Haanjoined the company in13-JAN-01and has218years experience
103AlexanderHunoldjoined the company in03-JAN-06and has160years experience
104BruceErnstjoined the company in21-MAY-07and has143years experience
105DavidAustinjoined the company in25-JUN-05and has166years experience
106ValliPataballajoined the company in05-FEB-06and has159years experience
107DianaLorentzjoined the company in07-FEB-07and has147years experience
108NancyGreenbergjoined the company in17-AUG-02and has200years experience
109DanielFavietjoined the company in16-AUG-02and has200years experience
110JohnChenjoined the company in28-SEP-05and has163years experience
111IsmaelSciarrajoined the company in30-SEP-05and has163years experience
112Jose ManuelUrmanjoined the company in07-MAR-06and has158years experience
113LuisPoppjoined the company in07-DEC-07and has137years experience
...
...

107 rows selected.


现在我想固定该间距,因此我通过串联添加了更多单个空间

SQL>
SQL>
SQL> SELECT employee_id || ' '|| first_name || ' '|| last_name ||
  2  ' joined the company in ' || hire_date || ' and has ' ||
  3  TRUNC((SYSDATE-hire_date)/31) || ' years experience' txt
  4  FROM hr.employees;

TXT
-------------------------------------------------------------------------------------------------------------------------------------------
100 Steven King joined the company in 17-JUN-03 and has 190 years experience
101 Neena Kochhar joined the company in 21-SEP-05 and has 163 years experience
102 Lex De Haan joined the company in 13-JAN-01 and has 218 years experience
103 Alexander Hunold joined the company in 03-JAN-06 and has 160 years experience
104 Bruce Ernst joined the company in 21-MAY-07 and has 143 years experience
105 David Austin joined the company in 25-JUN-05 and has 166 years experience
106 Valli Pataballa joined the company in 05-FEB-06 and has 159 years experience
107 Diana Lorentz joined the company in 07-FEB-07 and has 147 years experience
108 Nancy Greenberg joined the company in 17-AUG-02 and has 200 years experience
109 Daniel Faviet joined the company in 16-AUG-02 and has 200 years experience
110 John Chen joined the company in 28-SEP-05 and has 163 years experience
111 Ismael Sciarra joined the company in 30-SEP-05 and has 163 years experience
112 Jose Manuel Urman joined the company in 07-MAR-06 and has 158 years experience
113 Luis Popp joined the company in 07-DEC-07 and has 137 years experience
...
...

107 rows selected.


最后,如果我想要 * 年 * 的经验,那么我可能想除以365而不是31。


SQL>
SQL>
SQL> SELECT employee_id || ' '|| first_name || ' '|| last_name ||
  2  ' joined the company in ' || hire_date || ' and has ' ||
  3  TRUNC((SYSDATE-hire_date)/365) || ' years experience' txt
  4  FROM hr.employees;

TXT
-------------------------------------------------------------------------------------------------------------------------------------------
100 Steven King joined the company in 17-JUN-03 and has 16 years experience
101 Neena Kochhar joined the company in 21-SEP-05 and has 13 years experience
102 Lex De Haan joined the company in 13-JAN-01 and has 18 years experience
103 Alexander Hunold joined the company in 03-JAN-06 and has 13 years experience
104 Bruce Ernst joined the company in 21-MAY-07 and has 12 years experience
105 David Austin joined the company in 25-JUN-05 and has 14 years experience
106 Valli Pataballa joined the company in 05-FEB-06 and has 13 years experience
107 Diana Lorentz joined the company in 07-FEB-07 and has 12 years experience
108 Nancy Greenberg joined the company in 17-AUG-02 and has 16 years experience
109 Daniel Faviet joined the company in 16-AUG-02 and has 16 years experience
110 John Chen joined the company in 28-SEP-05 and has 13 years experience
111 Ismael Sciarra joined the company in 30-SEP-05 and has 13 years experience
112 Jose Manuel Urman joined the company in 07-MAR-06 and has 13 years experience
113 Luis Popp joined the company in 07-DEC-07 and has 11 years experience
...
...

107 rows selected.

SQL>
SQL>


就这样 :-)
文章转载自ASKTOM,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论