一次插入一行或多行數據html
語法: 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);
修改行數據mysql
注意:必定要有限制條件,不然將修改全部行的指定字段,會出生產事故的。正則表達式
可是也有方法規避sql
mysql 客戶端啓動時 增長參數 --safe-updates 或 -U ,固然,也能夠寫到[client]
使用限制條件 WHERE LIMIT
update test1 set sex='F' where sex='M';
全表更新ide
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的更新code
update test1 set sex='M' limit 1; Query OK, 1 row affected (0.52 sec)
加入where的更新htm
update test1 set sex='M' where id=2; Query OK, 1 row affected (0.01 sec)
可先排序再指定刪除的行數排序
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;
注意:必定要有限制條件,不然將清空表中的全部數據ci
字段 as 字段別名
過濾條件:布爾型表達式 算術操做符:+, -, *, /, % 比較操做符:=, !=, <>, <=>, >, >=, <, <= BETWEEN min_num AND max_num IN (列表) 從tb1表中查找出age等於2二、2三、24或25中的任意一個的行的全部數據 select * from tb1 where age in (22,23,24,25); NOT IN IS NULL IS NOT NULL
%: 任意長度的任意字符 _:任意單個字符 RLIKE:正則表達式,索引無效,不建議使用 select * from tb1 where name rlike '^t.*'; REGEXP:匹配字符串可用正則表達式書寫模式,同上
NOT AND OR XOR
升序: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關鍵字表示去重查詢
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 | +----------------+-------+-----------+
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;