select

單表查詢

測試表:company庫.employee員工5
僱員編號 id int
僱員姓名 name varchar(30)
僱員性別 sex enum
僱用時期 hire_date date
職位 post varchar(50)
職位描述 job_description varchar(100)
薪水 salary double(15,2)
辦公室 office int
部門編號 dep_id int

用edit---->複製粘貼保存
;

操作如下:
1.mysql<edit 回車
2.把下面的命令複製粘貼
3.保存退出然後iu打分號; 回車
4.再相同操作粘貼複製下面(插入記錄的那些命令)

創建表
mysql> CREATE TABLE company.employee5(
id int primary key AUTO_INCREMENT not null,
name varchar(30) not null,
sex enum('male','female') default 'male' not null,
hire_date date not null,
post varchar(50) not null,
job_description varchar(100),
salary double(15,2) not null,
office int,
dep_id int
);

插入記錄
mysql> insert into company.employee5(name,sex,hire_date,post,job_description,salary,office,dep_id) values
('jack','male','20180202','instructor','teach',5000,501,100),
('tom','male','20180203','instructor','teach',5500,501,100),
('robin','male','20180202','instructor','teach',8000,501,100),
('alice','female','20180202','instructor','teach',7200,501,100),
('tianyun','male','20180202','hr','hrcc',600,502,101),
('harry','male','20180202','hr',NULL,6000,502,101),
('emma','female','20180206','sale','salecc',20000,503,102),
('christine','female','20180205','sale','salecc',2200,503,102),
('zhuzhu','male','20180205','sale',NULL,2200,503,102),
('gougou','male','20180205','sale','',2200,503,102);

======================================
開始:
mysql> select 字段名稱,字段名稱2 from 表名 條件

簡單查詢:
mysql> select * from t3;
mysql> select name , salary薪水, dep_id from employee5;
mysql> select name as mingzi, salary薪水 as moeny , dep_id from employee5;
mysql> select name as mingzi, salary+dep_id as mun from employee5;
as可以不寫

避免重複DISTINCT
SELECT post FROM employee5;
SELECT DISTINCT post FROM employee5; ---去重
注:不能部分使用DISTINCT,通常僅用於某一字段。

通過四則運算查詢
shell中調用mysql:第一種方法:mysql -e "select 32747*43868" | tail -1
第二種方法:
mysql <<eof

eof

運算:
mysql>select math*china-50 from t1;
mysql>select 437.4384/5
mysql>select 5>3;  (true爲1)

SELECT name, salary, salary*14 FROM employee5;
SELECT name, salary, salary*14 AS Annual_salary FROM employee5;
SELECT name, salary, salary*14 Annual_salary FROM employee5;

定義顯示格式
CONCAT() 函數用於連接字符串(連接在一起)
SELECT CONCAT(name, ' annual salary: ', salary*14) AS Annual_salary FROM employee5;

單條件查詢
mysql> select pass from t3 where name="wing";
where not name= "wing" 除了wing的

多條件查詢
mysql>select math from db1.t1 where math>50 and math<600;and也可以是&&
mysql>select math from db1.t1 where not math>50;

關鍵字BETWEEN AND
    SELECT name,salary FROM employee5 
        WHERE salary BETWEEN 5000 AND 15000;

    SELECT name,salary FROM employee5 
        WHERE salary NOT BETWEEN 5000 AND 15000;

關鍵字IS NULL
    SELECT name,job_description FROM employee5 
        WHERE job_description IS NULL;

    SELECT name,job_description FROM employee5 
        WHERE job_description IS NOT NULL;

    SELECT name,job_description FROM employee5 
        WHERE job_description='';

    NULL說明:
    1、等價於沒有任何值、是未知數。
   2、NULL與0、空字符串、空格都不同,NULL沒有分配存儲空間。
    3、對空值做加、減、乘、除等運算操作,結果仍爲空。
    4、比較時使用關鍵字用“is null”和“is not null”。
    5、排序時比其他數據都小(索引默認是降序排列,小→大),所以NULL值總是排在最前。

關鍵字IN集合查詢
    SELECT name, salary FROM employee5 
        WHERE salary=4000 OR salary=5000 OR salary=6000 OR salary=9000 ;

    SELECT name, salary FROM employee5 
        WHERE salary IN (4000,5000,6000,9000) ;

    SELECT name, salary FROM employee 
        WHERE salary NOT IN (4000,5000,6000,9000) ;

排序查詢

mysql> select china from t1 order by china;
mysql> select china from t1 order by china desc;  降序
mysql> select china from t1 order by china desc limit 3;  降序前三名
mysql> select china from t1 order by china desc limit 1,3;從第2行開始取三個

注:
ascending    美音 /ə'sɛndɪŋ/   升序(默認)
descending  美音 /dɪ'sɛndɪŋ/  降序

按多列排序:
    入職時間相同的人薪水不同
    SELECT * FROM employee5 
        ORDER BY hire_date DESC, 
        salary ASC;

先按入職時間,再按薪水排序

先按職位,再按薪水排序

限制查詢的記錄數
    SELECT * FROM employee5 ORDER BY salary DESC 
        LIMIT 5;                            //默認初始位置爲0 

    SELECT * FROM employee5 ORDER BY salary DESC
        LIMIT 0,5;

    SELECT * FROM employee5 ORDER BY salary DESC
        LIMIT 3,5;                          //從第4條開始,共顯示5條

開始:
分組查詢
mysql> select count(gender),gender from t3 group by gender;
統計男女各多少人

GROUP BY和GROUP_CONCAT()函數一起使用(把不同組的記錄放到一行)
SELECT dep_id,GROUP_CONCAT(name) FROM employee5 GROUP BY dep_id;
SELECT dep_id,GROUP_CONCAT(name) as emp_members FROM employee5 GROUP BY dep_id;

GROUP BY和集合函數SUM(salary)一起使用   -------計算每個部門總共多少錢

模糊查詢(通配符)
_ 任意單個字符

%  所有字符

mysql> select * from t1 where china='1__';
mysql> select * from t1 where china like '%0%';

正則查詢
mysql> select from t1 where china regexp '10+';
SELECT
FROM employee5 WHERE name REGEXP '^ali';
SELECT FROM employee5 WHERE name REGEXP 'yun$';
SELECT
FROM employee5 WHERE name REGEXP 'm{2}';

子查詢
mysql> select name from t2 where math=(select max(math) from t2);

函數
count()
max()
min()
avg()
database()
user()
now()
sum()

password()
md5()
sha1()
power()

SELECT COUNT(*) FROM employee5;
SELECT COUNT(*) FROM employee5 WHERE dep_id=101;
SELECT MAX(salary) FROM employee5;
SELECT MIN(salary) FROM employee5;
SELECT AVG(salary) FROM employee5;
SELECT SUM(salary) FROM employee5;
SELECT SUM(salary) FROM employee5 WHERE dep_id=101; 

MariaDB [company]> select password(5);
+-------------------------------------------+
| password(5)                               |
+-------------------------------------------+
| *7534F9EAEE5B69A586D1E9C1ACE3E3F9F6FCC446 |
+-------------------------------------------+

MariaDB [company]> select md5(5);
+----------------------------------+
| md5(5)                           |
+----------------------------------+
| e4da3b7fbbce2345d7772b0674a318d5 |
+----------------------------------+
1 row in set (0.00 sec)

MariaDB [company]> select sha1(5);
+------------------------------------------+
| sha1(5)                                  |
+------------------------------------------+
| ac3478d69a3c81fa62e60f5c3696165a4e5e6ac4 |
+------------------------------------------+
1 row in set (0.00 sec)

POWER() function
MySQL POWER() returns the value of a number raised to the power of another number. The synonym of POWER() is POW().

mysql> SELECT POWER(3, 2);
+-------------+
| POWER(3, 2) |
+-------------+
|           9 | 
+-------------+
1 row in set (0.00 sec)

小結:對字符串匹配的方式
WHERE name = 'tom';
WHERE name LIKE 'to%';
WHERE name REGEXP 'yun$';

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章