mysql常用命令/語句學習一
mysql的常用命令/語句太多, 會分幾篇來簡介.
本篇簡介的是:
select命令(巨常用,後面還會講到)
update命令
insert into命令
delete/drop命令
rename命令
where子語句
like子語句
mysql排序
一: select命令
SELECT what_to_select
FROM which_table
WHERE conditions_to_satisfy;
1.查詢所有資料:
mysql> SELECT * FROM pet;
+----------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Bowser | Diane | dog | m | 1979-08-31 | NULL |
| Chirpy | Gwen | bird | f | 1997-12-09 | NULL |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
+----------+--------+---------+------+------------+-------+
9 rows in set (0.00 sec)
2.查詢行
mysql> SELECT * FROM pet WHERE name = 'Bowser';
+--------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+--------+-------+---------+------+------------+-------+
| Bowser | Diane | dog | m | 1979-08-31 | NULL |
+--------+-------+---------+------+------------+-------+
1 row in set (0.00 sec)
3.查詢列:
mysql> select name, sex from pet;
+----------+------+
| name | sex |
+----------+------+
| Fluffy | f |
| Claws | m |
| Buffy | f |
| Fang | m |
| Bowser | m |
| Chirpy | f |
| Whistler | NULL |
| Slim | m |
| Puffball | f |
+----------+------+
9 rows in set (0.00 sec)
注意: 增加關鍵字DISTINCT檢索出每個唯一的輸出記錄:
mysql> select distinct owner from pet;
+--------+
| owner |
+--------+
| Harold |
| Gwen |
| Benny |
| Diane |
+--------+
4 rows in set (0.00 sec)
mysql> select owner from pet;
+--------+
| owner |
+--------+
| Harold |
| Gwen |
| Harold |
| Benny |
| Diane |
| Gwen |
| Gwen |
| Benny |
| Diane |
+--------+
9 rows in set (0.00 sec)
二: update命令
需要更改某條記錄的值, UPDATE只更改指定的記錄,不需要重新轉載資料表。
UPDATE table_name SET field1=new-value1, field2=new-value2
[WHERE Clause]
例:
mysql> update pet set sex = 'f' where name = "Chirpy";
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
三: insert into 命令
在上一篇mysql資料庫/資料表的建立/檢視 裡面也提到過了, 像資料表中插入新定義的記錄.
insert into 還可以加上select命令完已存在的資料表中插入多個記錄.
mysql> select * from pet1;
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
1 row in set (0.00 sec)
mysql> insert into pet1 select * from pet where sex = "f";
Query OK, 4 rows affected (0.04 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from pet1;
+----------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+-------+
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Chirpy | Gwen | bird | f | 1997-12-09 | NULL |
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
+----------+--------+---------+------+------------+-------+
5 rows in set (0.00 sec)
四: delete/drop命令
DELETE FROM table_name [WHERE Clause]
利用where語句刪除滿足條件的若干個記錄, 如果沒有where語句,則刪除整個資料表.
drop命令是直接用來刪除資料表/資料庫的.
drop database database-name
drop table table-name
而如果是用
DELETE FROM table_name
DELETE FROM database_name
這是清空的意思. 並不會刪除資料庫/資料表本身.
五: rename命令
RENAME DATABASE db_name TO new_db_name;
RENAME TABLE db_name.table1 TO db_name.table2;
六: where子語句
可用於select/delete/update命令中, 指定任何條件.
例:
mysql> SELECT * FROM pet WHERE birth > '1998-1-1';
+----------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+-------+
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
+----------+-------+---------+------+------------+-------+
1 row in set (0.00 sec)
可以組合條件: 使用AND邏輯操作符, 或者一個OR操作符;
mysql> SELECT * FROM pet WHERE species = 'dog' AND sex = 'f';
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
1 row in set (0.00 sec)
mysql> SELECT * FROM pet WHERE species = 'snake' OR species = 'bird';
+----------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+-------+
| Chirpy | Gwen | bird | f | 1997-12-09 | NULL |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
+----------+-------+---------+------+------------+-------+
3 rows in set (0.00 sec)
AND和OR可以混用,但AND比OR具有更高的優先順序。
如果使用兩個操作符,需要使用括號指明如何條件分組
mysql> SELECT * FROM pet WHERE (species = 'cat' AND sex = 'm')
-> OR (species = 'dog' AND sex = 'f');
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
2 rows in set (0.00 sec)
七: like子語句
SELECT field1, field2,...fieldN
FROM table_name
WHERE field1 LIKE condition1 [AND [OR]] filed2 = 'somevalue'
你可以使用LIKE子句代替等號 =。
LIKE 通常與%一同使用,類似於一個元字元的搜尋。
例:
mysql> select * from pet where name like "%y";
+--------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Chirpy | Gwen | bird | f | 1997-12-09 | NULL |
+--------+--------+---------+------+------------+-------+
3 rows in set (0.00 sec)
八: mysql排序
將查詢結果行排序後輸出結果
SELECT field1, field2,...fieldN table_name1, table_name2...
ORDER BY field1, [field2...][ASC [DESC]]
使用 ASC 或 DESC 關鍵字來設定查詢結果是按升序或降序排列。 預設情況下,它是按升序排列。
可以新增 WHERE…LIKE 子句來設定條件。
例:
mysql> SELECT name, birth FROM pet ORDER BY birth;
+----------+------------+
| name | birth |
+----------+------------+
| Bowser | 1979-08-31 |
| Buffy | 1989-05-13 |
| Fang | 1990-08-27 |
| Fluffy | 1993-02-04 |
| Claws | 1994-03-17 |
| Slim | 1996-04-29 |
| Chirpy | 1997-12-09 |
| Whistler | 1997-12-09 |
| Puffball | 1999-03-30 |
+----------+------------+
9 rows in set (0.00 sec)