1. 程式人生 > >oracle和mysql的sql語法的區別和聯絡

oracle和mysql的sql語法的區別和聯絡

      oracle資料庫很多操作和mysql差不多,oracle是最早使用sql查詢語言的資料庫,有mysql開發經驗的開發人員上手oracle還是很快的。

1.首先oracl運算子相關的計算和mysql一樣,舉例,查詢時間sysdate,運算子,別名

SQL> select sysdate ,sysdate+1,sysdate-1 , 8+3*2 as dengyu from dual;
SYSDATE     SYSDATE+1   SYSDATE-1       DENGYU
----------- ----------- ----------- ----------
2018-03-18  2018-03-19  2018-03-17          14

2.連線符,用 ‘||’表示:把列與列,列與字元連線在一起(注意別名用“”則區分大小寫)

SQL> select  last_name || first_name as "NaMe" from employees ;
NaMe
---------------------------------------------
KingSteven
KochharNeena
De HaanLex
HunoldAlexander
ErnstBruce
---------------------------------------------
SQL> SELECT last_name	||' is a '||job_id
  2         AS "Employee Details"
  3  FROM   employees;
Employee Details
-----------------------------------------
King is a AD_PRES
Kochhar is a AD_VP
De Haan is a AD_VP
Hunold is a IT_PROG
Ernst is a IT_PROG
Austin is a IT_PROG
Pataballa is a IT_PROG

3.同樣distinct的使用方式和mysql中一樣,都是去重。select distinct(name) from a.同樣desc A查看錶結構

4. 查詢資料庫中所有表.

      select table_name from dba_tables  where owner='orcl001';

      select table_name from dba_tables 

5.其餘between ..and ,in ,not in ,and ,or,is null ,like等使用和mysql一致。

SQL> SELECT last_name, salary
  2  FROM   employees
  3  WHERE  salary BETWEEN 2500 AND 3500;
LAST_NAME                     SALARY
------------------------- ----------
Khoo                         3100.00
Baida                        2900.00
Tobias                       2800.00
Himuro                       2600.00
Colmenares                   2500.00

SQL> SELECT employee_id, last_name, salary, manager_id
  2  FROM   employees
  3  WHERE  manager_id IN (100, 101, 201);
EMPLOYEE_ID LAST_NAME                     SALARY MANAGER_ID
----------- ------------------------- ---------- ----------
        101 Kochhar                     17000.00        100
        102 De Haan                     17000.00        100
        108 Greenberg                   12000.00        101
        114 Raphaely                    11000.00        100
        120 Weiss                        8000.00        100
        121 Fripp                        8200.00        100

SQL> SELECT	first_name
  2  FROM 	employees
  3  WHERE	first_name LIKE 'S%';
FIRST_NAME
--------------------
Steven
Shelli
Sigal
Shanta
Steven
Stephen  迴避特殊符號的:使用轉義符。例如:將[%]轉為[\%]、[_]轉為[\_],然後再加上[ESCAPE ‘\’] 即可
6.order by 排序和mysql使用一樣,asc 升序,desc降序
SQL> SELECT   last_name, job_id, department_id, hire_date
  2  FROM     employees
  3  ORDER BY hire_date ;
LAST_NAME                 JOB_ID     DEPARTMENT_ID HIRE_DATE
------------------------- ---------- ------------- -----------
King                      AD_PRES               90 1987-06-17
Whalen                    AD_ASST               10 1987-09-17
Kochhar                   AD_VP                 90 1989-09-21
Hunold                    IT_PROG               60 1990-01-03
Ernst                     IT_PROG               60 1991-05-21
De Haan                   AD_VP                 90 1993-01-13

7.其餘的oracle函式,單行函式,多行函式和mysql使用一致

SQL> SELECT last_name, job_id, salary,
  2         CASE job_id WHEN 'IT_PROG'  THEN  1.10*salary
  3                     WHEN 'ST_CLERK' THEN  1.15*salary
  4                     WHEN 'SA_REP'   THEN  1.20*salary
  5         ELSE      salary END     "REVISED_SALARY"
  6  FROM   employees;
LAST_NAME                 JOB_ID         SALARY REVISED_SALARY
------------------------- ---------- ---------- --------------
King                      AD_PRES      24000.00          24000
Kochhar                   AD_VP        17000.00          17000
De Haan                   AD_VP        17000.00          17000
Hunold                    IT_PROG       9000.00           9900
Ernst                     IT_PROG       6000.00           6600
Austin                    IT_PROG       4800.00           5280
Pataballa                 IT_PROG       4800.00           5280
Lorentz                   IT_PROG       4200.00           4620
Greenberg                 FI_MGR       12000.00          12000
.............................

需要使用if-elseif-else格式時,可以使用deocode替換。
DECODE(col|expression, search1, result1 ,
      			   [, search2, result2,...,]
      			   [, default])
SQL> SELECT last_name, job_id, salary,
  2         DECODE(job_id, 'IT_PROG',  1.10*salary,
  3                        'ST_CLERK', 1.15*salary,
  4                        'SA_REP',   1.20*salary,
  5                			    salary)
  6         REVISED_SALARY
  7  FROM   employees;
LAST_NAME                 JOB_ID         SALARY REVISED_SALARY
------------------------- ---------- ---------- --------------
King                      AD_PRES      24000.00          24000
Kochhar                   AD_VP        17000.00          17000
De Haan                   AD_VP        17000.00          17000
Hunold                    IT_PROG       9000.00           9900
Ernst                     IT_PROG       6000.00           6600
Austin                    IT_PROG       4800.00           5280
Pataballa                 IT_PROG       4800.00           5280
Lorentz                   IT_PROG       4200.00           4620
Greenberg                 FI_MGR       12000.00          12000
Faviet                    FI_ACCOUNT    9000.00           9000
...............................
8.oracle的join系列和mysql基本一樣。