1. 程式人生 > 其它 >小白一看就會的基本SELECT語句

小白一看就會的基本SELECT語句

第03章_基本的SELECT語句

1.SQL概述

1.1SQL背景知識

1.2SQL語言排行榜

自從SQL加入了TIOBE程式語言排行榜,就一直保持在Top 10.

1.3 SQL分類

SQL語言在功能上主要分為如下3大類:

學習技巧:大出著眼,小處著手。

2.SQL語言的規則與規範

2.1基本規則

2.2 SQL大小寫規範(建議遵守)

2.3註釋

2.4命名規則(暫時瞭解)

2.5匯入資料

方式一:source 檔案的全路徑名

舉例:source D:\atguigudb.sql;

mysql> source D:\atguigudb.sql;
ERROR:
Unknown command '\a'.
Query OK, 0 rows affected, 1 warning (0.04 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 1 row affected, 1 warning (0.30 sec)

Database changed
Query OK, 0 rows affected (0.10 sec)

Query OK, 0 rows affected, 2 warnings (5.29 sec)

Query OK, 25 rows affected (0.50 sec)
Records: 25  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.18 sec)

Query OK, 0 rows affected, 4 warnings (1.84 sec)

Query OK, 27 rows affected (0.20 sec)
Records: 27  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.10 sec)

Query OK, 0 rows affected, 6 warnings (4.07 sec)

Query OK, 107 rows affected (0.62 sec)
Records: 107  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.12 sec)

Query OK, 0 rows affected, 3 warnings (2.30 sec)

Query OK, 6 rows affected (0.36 sec)
Records: 6  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.21 sec)

Query OK, 0 rows affected, 3 warnings (1.51 sec)

Query OK, 10 rows affected (0.33 sec)
Records: 10  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.22 sec)

Query OK, 0 rows affected, 3 warnings (2.03 sec)

Query OK, 19 rows affected (0.29 sec)
Records: 19  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.15 sec)

Query OK, 0 rows affected, 2 warnings (2.00 sec)

Query OK, 23 rows affected (0.48 sec)
Records: 23  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.29 sec)

Query OK, 0 rows affected, 2 warnings (1.63 sec)

Query OK, 3 rows affected (0.36 sec)
Records: 3  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.19 sec)

Query OK, 0 rows affected, 2 warnings (3.94 sec)

Query OK, 4 rows affected (0.57 sec)
Records: 4  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.29 sec)

Query OK, 0 rows affected (0.23 sec)

Query OK, 0 rows affected (0.07 sec)

Query OK, 0 rows affected, 6 warnings (2.69 sec)

Query OK, 0 rows affected (2.01 sec)

Query OK, 0 rows affected (0.18 sec)

Query OK, 0 rows affected (0.93 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| atguigudb          |
| dbtest1            |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.11 sec)

方式二:基於具體的圖形化介面的工具可以匯入資料庫

3.基本的SELECT語句

3.0SELECT...

SELECT 1;  #沒有任何子句
SELECT 9/2 
REOM DUAL; #dual:偽表

3.1SELECT ... FROM

  • 語法:SELECT 欄位1,欄位2,... FROM 表名
# *:表中的所有欄位(或列)
SELECT * FROM employees;

SELECT employees_id,last_name,salary
FROM employees;

3.2列的別名

3.3去除重複行

3.4空值參與運算

3.5著重號


3.6查詢常數

4.顯示錶結構

使用DESCRIBE或DESC,命令,表示表結構。

DESCRIBE employees;
或
DESC employees;

5.過濾資料

  • 背景
  • 語法
#查詢90號部門的員工資訊
SELECT * 
FROM employees
#過濾條件宣告在from後面
WHERE department_id = 90;

課後習題

【題目】1.查詢員工12個月的工資總和,並起別名為ANNUALSALARY

2.查詢employees表中去除重複的job_id以後的資料

3.查詢工資大於12000的員工姓名和工資

4.查詢員工號為176的員工的姓名和部門號

5.顯示錶departments的結構,並查詢其中的全部資料

#第03章課後練習題

1.查詢員工12個月的工資總和,並起別名為ANNUALSALARY
#基本工資,無獎金

SELECT employee_id,last_name,salary * 12 "ANNUL SALARY"
FROM employees;

所有工資,包括獎金

SELECT employee_id,last_name,salary * 12*(1+IFNULL(commission_pct,0)) "ANNUL SALARY" 
FROM employees;


2.查詢employees表中去除重複的job_id以後的資料

SELECT DISTINCT job_id
FROM employees;


3.查詢工資大於12000的員工姓名和工資
SELECT last_name,salary
FROM employees
WHERE salary  >12000;

4.查詢員工號為176的員工的姓名和部門號

SELECT last_name,department_id
FROM employees
WHERE employee_id = 176;

#5.顯示錶departments的結構,並查詢其中的全部資料

DESCRIBE departments;