Oracle's basic query, filtering and sorting query-day02

5 Oracle's basic query, filtering and sorting query

5.1 Solve the problem of Chinese garbled characters in SqlPlus

Insert picture description here

  • Previously used hr login, now use another login set
  • Use scott account to log in [sqlplus scott/[email protected]:1521/orcl]
    Insert picture description here
  • Chinese garbled problems were found when creating aliases
    Insert picture description here

first step

  • Execute select userenv('language') from dual in sqlplus;
  • View the character set of the current database: AMERICAN_AMERICA.ZHS16GBK
    Insert picture description here
  • We only need to set both the client character set and operating system character set to: SIMPLIFIED CHINESE_CHINA.ZHS16GBK.

Second step

  • Find an environment variable named "NLS_LANG" in the system environment variables. If it is not found, create a new one and assign the variable value to: "SIMPLIFIED CHINESE_CHINA.ZHS16GBK"
    Insert picture description here
  • Close the dos terminal, rewrite and open (Chinese garbled problem solved)
    Insert picture description here

5.2 Oracle basic query statement

spool save sql to file

  1. Save the sql statement to the file [spool C:\Users\shuyy\Desktop\a.txt]
  2. spool off write content to the file [to write the SQL that needs to be saved to the file between spool C:\Users\shuyy\Desktop\a.txt and spool off, after the spool off is closed, the SQL written again will not Write to specified file]
    Insert picture description here

host cls clear screen

Insert picture description here

show user shows the currently logged in user

Insert picture description here

select * from tab; Display the table under the current user

  • Unlike the MySQL database, Oracle does not display the database like MySQL, and then use
    Insert picture description here
  • The Oracle database will directly display the tables that can be seen by the current user permissions [the tables here are some of the tables provided by Oracle by default]
    Insert picture description here

desc query table structure [desc emp;]

Insert picture description here

show linesize show line width

  • Query the emp table and found that the display is not beautiful because of the row width problem
    Insert picture description here
    Insert picture description here

set linesize 150 set line width

  • Much more beautiful
    Insert picture description here

col ename for a8 Set the width of ename column, display 8 characters, a represents characters

Insert picture description here
Insert picture description here

col sal for 9999 set sal column to display 4 numbers, 9 means number

Insert picture description here

  • 999 means that 3 numbers are displayed, and the data of 4 numbers is found to become ####
    Insert picture description here

/ Means to execute the previous statement

Insert picture description here

c Change command, use when typing a wrong sentence [eg: c /form/from]

Insert picture description here

* + Multiplication and addition operations

#查询员工信息: 员工号  姓名 月薪 
select empno "员工号", ename "名字",sal "月薪" from emp;

Insert picture description here

#查询员工信息: 员工号  姓名 月薪 年薪 奖金 年收入
select empno "员工号", ename "名字",sal "月薪", sal*12 "年薪",comm "奖金", sal*12+comm "年收入" from emp;
  • [It is found that there is a problem with this query. The result of adding the annual salary and bonus data to null is null, obviously this does not meet the requirements]
    Insert picture description here
#让comm里面的null变成0【nvl(comm,0)】
select empno "员工号", ename "名字",sal "月薪", sal*12 "年薪",comm "奖金", sal*12+nvl(comm,0) "年收入" from emp;

Insert picture description here

Query the null value in the table

  • MySQL can be written like this [writing like this in Oracle will report an error]: select * from emp where comm = null;
  • select * from emp where comm is null;
    Insert picture description here

alias of as column

select empno as "员工号", ename "名字",sal "月薪", sal*12 "年薪",comm "奖金", sal*12+nvl(comm,0) "年收入" from emp;

Insert picture description here

ed into the write cache file file afiedt.buf [convenient to modify long SQL statements]

Insert picture description here

  • After the modification is completed, save with Ctrl+s, then close the file, and enter / to indicate the execution of the last SQL statement
    Insert picture description here

distinct 1. Remove duplicates 2. Act on all subsequent columns

#去除重复
select distinct deptno from emp;

Insert picture description here

#作用于后面所有的列
 select distinct deptno, job from emp;

Insert picture description here

concat string concatenation

  • dual represents a virtual table
select concat('abc','def') from dual;

Insert picture description here

dual represents a virtual table

select 3+2 from dual;

Insert picture description here

|| also means string concatenation

select 'abc'||'aaa' from dual;

Insert picture description here

 select ename|| '的工资是'||sal from emp;

Insert picture description here
Insert picture description here
Insert picture description here

5.3 Oracle's filtering and sorting query

Character case sensitivity

#如查询员工为SMITH的信息
select * from emp where ename='SMITH';
select * from emp where ename='SMITh';

Insert picture description here

Date format is sensitive

#查看当前的日期格式
select * from v$nls_parameters;
#修改当前会话的日期格式【下一次打开还是原来的格式】
#java中是yyyy-MM-dd
alter session set NLS_DATE_FORMAT='yyyy-mm-dd';
#查询日期
select * from emp where hiredate='17-12月-80';
select * from emp where hiredate='1980-12-17';

Insert picture description here
Insert picture description here

between and

#查询薪水1000~2000之间的员工,结果是包含1000和2000的
select * from emp where sal between 1000 and 2000;
#注:含有边界 小值在前 大值在后

Insert picture description here

in is in the set, not in is not in the set

#查询10和20号部门的员工
select * from emp where deptno in(10,20);
#查询不在10和20号部门的员工
select * from emp where deptno not in(10,20);
#注:如果集合中含有null,不能使用not in; 但可以使用in

Insert picture description here
Insert picture description here

like fuzzy query [similar to mysql]

#查询名字以S打头的员工
select * from emp where ename like 'S%';
#查询名字是4个字的员工,4个下划线即可
select * from emp where ename like '____';
#查询名字中含有下划线的员工 需要转义字符
select * from emp where ename like '%\_%' escape '\';

Insert picture description here

rollback

  • Oracle starts transactions automatically, unlike mysql, which needs to be started manually [start transaction;]

order by

#1.查询员工信息 按照月薪排序【默认升序】
select * from emp order by sal;
#2.按年薪降序排序【order by后面  + 列,表达式,别名,序号】
select empno,ename,sal,sal*12 from emp order by sal*12 desc;

Insert picture description here

#根据别名排序
select empno,ename,sal,sal*12 年薪 from emp order by 年薪 desc;
#根据列数排序
select empno,ename,sal,sal*12 年薪 from emp order by 9 desc;

Insert picture description here
Insert picture description here

#3.按部门升序然后按工资降序排序员工信息
select * from emp order by deptno,sal desc;
#4.按部门降序然后按工资降序排序员工信息
select * from emp order by deptno desc,sal desc;
#注:order by 作用于后面所有的列;desc只作用于离它最近的列

Insert picture description here

#6.查询员工信息  按照奖金降序排序,null值是最大,所以会排在最前面
select * from emp order by comm desc;
select * from emp order by comm desc nulls last;

Insert picture description here

set pagesize set page size

#设置分页大小
set pagesize 20;

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43414199/article/details/109258473