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;
原文地址:http://blog.51cto.com/191226139/2092003
时间: 2024-10-18 02:40:52