1. 程式人生 > >mysql(四)-增刪改查

mysql(四)-增刪改查

mysql

INSERT

一次插入一行或多行數據

語法:
INSERT into [(字段1,字段2...)] VALUES (字段1值,字段2值...), (val21,...)
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

如果有自動遞增屬性auto_increment,會以新插入的自動遞增項最後以起始。
http://dev.mysql.com/doc/refman/5.5/en/insert.html
inser into test1 (id,name,sex) values (1,‘zhangshan‘,‘M‘);
字段與值要一一對應

向表tb1中插入多條數據,具體含義同上,只不過是插入多條語句
insert into tb1 (name,age) values(‘jerry‘,22),(‘naruto‘,28);

也可以不指定字段,表示對應每個字段都會有插入的數據。
insert into tb1 values (4,‘Sasuke‘,28),(5,‘hinata‘,25);

UPDATE

修改行數據

註意:一定要有限制條件,否則將修改所有行的指定字段,會出生產事故的。

但是也有方法規避

mysql 客戶端啟動時 增加參數 --safe-updates 或 -U ,當然,也可以寫到[client]
使用限制條件
WHERE
LIMIT
update test1 set sex=‘F‘ where sex=‘M‘;

全表更新

update test1 set sex=‘M‘;
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

加入limit的更新

update test1 set sex=‘M‘ limit 1;
Query OK, 1 row affected (0.52 sec)

加入where的更新

update test1 set sex=‘M‘ where id=2;
Query OK, 1 row affected (0.01 sec)

DELETE

可先排序再指定刪除的行數

ORDER BY ...

限制行數

LIMIT
delete from test1 where id =1;

從tb1表中找出age>30的數據行,然後將這些行按照age進行降序排列,排列後刪除第一個
delete from tb1 where age > 30 order by age desc limit 1;

?註意:一定要有限制條件,否則將清空表中的所有數據

SELECT

使用別名

字段 as 字段別名

WHERE子句:指明過濾條件以實現“選擇”的功能:

過濾條件:布爾型表達式
算術操作符:+, -, *, /, %
比較操作符:=, !=, <>, <=>, >, >=, <, <=
BETWEEN min_num AND max_num
IN (列表)
    從tb1表中查找出age等於22、23、24或25中的任意一個的行的所有數據
    select * from tb1 where age in (22,23,24,25);
NOT IN 
IS NULL
IS NOT NULL

LIKE 與 RLIKE:

%: 任意長度的任意字符
_:任意單個字符
RLIKE:正則表達式,索引無效,不建議使用
    select * from tb1 where name rlike ‘^t.*‘;
REGEXP:匹配字符串可用正則表達式書寫模式,同上

邏輯操作符

NOT
AND
OR
XOR

order by 根據指定的字段對查詢結果進行排序

升序:ASC   默認
降序:DESC

select name,code2,indepyear from country where indepyear NOT in (1990,1800,1993) order by indepyear asc;

如果多行之間的age字段的值相同時,這些行再根據name字段進行升序排序
select * from tb1 order by age desc,name asc;

把NULL排序到最後,在字段名前用-,排序方法desc
order by -indepyear desc

DISTINCT 去重查詢

查詢某字段的時候去重,使用DISTINCT關鍵字表示去重查詢

select distinct indepyear from country order by indepyear desc;
+-----------+
| indepyear |
+-----------+
|      1994 |
|      1993 |
|      1992 |
|      1991 |
|      1990 |

查詢city表中的所有數據,如果表中的數據量巨大,一般不要這樣對數據進行查詢

select * from city;

從city表中查詢出所有數據,但是只顯示前3行

select * from city limit 3;
+----+----------+-------------+----------+------------+
| ID | Name     | CountryCode | District | Population |
+----+----------+-------------+----------+------------+
|  1 | Kabul    | AFG         | Kabol    |    1780000 |
|  2 | Qandahar | AFG         | Qandahar |     237500 |
|  3 | Herat    | AFG         | Herat    |     186800 |
+----+----------+-------------+----------+------------+

從city表中查詢出數據,只顯示字段name,district,countrycode數據,查詢匹配的條件為countrycode=‘AFG‘

select name,district,countrycode from city where countrycode=‘AFG‘;
+----------------+----------+-------------+
| name           | district | countrycode |
+----------------+----------+-------------+
| Kabul          | Kabol    | AFG         |
| Qandahar       | Qandahar | AFG         |
| Herat          | Herat    | AFG         |
| Mazar-e-Sharif | Balkh    | AFG         |
+----------------+----------+-------------+

從country表中查詢出數據,只顯示字name,code2,indepyear數據,查詢匹配的條件為indepyear > ‘1992‘

select name,code2,indepyear from country where indepyear > ‘1992‘;
+----------------+-------+-----------+
| name           | code2 | indepyear |
+----------------+-------+-----------+
| Czech Republic | CZ    |      1993 |
| Eritrea        | ER    |      1993 |
| Palau          | PW    |      1994 |
| Slovakia       | SK    |      1993 |
+----------------+-------+-----------+

分組與聚合

GROUP 分組的目的往往是對分組後的數據進行"聚合操作"

avg()   返回指定列的平均值
max()   返回指定列的最大值
min()   返回指定列的最小值
count() 返回指定列中非null值的個數
sum()   返回指定列的所有值之和

select count(Language) from countrylanguage group by CountryCode limit 10;
+-----------------+
| count(Language) |
+-----------------+
|               4 |
|               5 |
|               9 |
|               1 |
|               3 |
|               4 |

HAVING: 對分組聚合運算後的結果指定過濾條件

select count(Language) from countrylanguage group by CountryCode having count(Language) > 5 limit 10;
+-----------------+
| count(Language) |
+-----------------+
|               9 |
|               8 |
|               8 |
|               6 |
|               7 |
|               6 |
|               7 |
|               6 |
|              12 |
|              12 |
+-----------------+

查詢students表,以性別為分組,求出分組後的年齡之和。
select gender,sum(age) from students group by gender;

查詢students表,以classid分組,顯示平均年齡大於25的classid。
select classid,avg(age) as avgage from students group by classid having avgage > 25;

查詢students表,以性別字段gender分組,顯示各組中年齡大於19的學員的年齡的總和。
select sum(age) from students where age > 19 group by gender;

mysql(四)-增刪改查