//模糊查询
mysql> select * from mingjue where name like '小%';
+----+--------+------+----------+| id | name | age | salary |
+----+--------+------+----------+
| 7 | 小白龙 | 12 | 10000.09 |
| 8 | 小黑龙 | 12 | 10000.09 |
| 9 | 小黄龙 | 12 | 10000.09 |
| 10 | 小神龙 | 12 | 10000.09 |
| 11 | 小甜甜 | 12 | 10000.09 |
+----+--------+------+----------+
//模糊查询 加限制个数 LIMIT x,x 两个参数 表示从第几个之后取几个。
mysql> select * from mingjue where name like '小%' LIMIT 2;
+----+--------+------+----------+
| id | name | age | salary |
+----+--------+------+----------+
| 7 | 小白龙 | 12 | 10000.09 |
| 8 | 小黑龙 | 12 | 10000.09 |
+----+--------+------+----------+
//根据name排序
mysql> SELECT * FROM mingjue where name like '%龙%' ORDER BY NAME;
+----+--------+------+----------+
| id | name | age | salary |
+----+--------+------+----------+
| 7 | 小白龙 | 12 | 10000.09 |
| 10 | 小神龙 | 12 | 10000.09 |
| 9 | 小黄龙 | 12 | 10000.09 |
| 8 | 小黑龙 | 12 | 10000.09 |
| 5 | 白龙马 | 5 | 7000.19 |
+----+--------+------+----------+
MySQL的聚合函数
----只能有一个结果
1.max(列名)
计算某列的最大值
2.min(列名)
计算某列的最小值
3.avg(列名)
计算某列的平均值
SELECT avg(salary) from mingjue;
4.sum(列名)
计算某列的总和
5.count(*)与count(列名)
计算记录总数或某列的记录数量。
count(*)查询所有记录总数,无论是否为null。
count(列名)只查询指定列中不为null的记录总数。
6.四舍五入 round(zhi,num);
//子查询
SELECT name from mingjue where salary>(SELECT round(avg(salary),2) from mingjue);
笛卡尔积
select * from mingjue,yaoguai;
mysql> select * from emp;
+----+------+------+
| id | name | dept |
+----+------+------+
| 1 | 三丰 | 1 |
| 2 | 无忌 | 2 |
| 3 | 芷若 | 2 |
| 4 | 灭绝 | NULL |
+----+------+------+
4 rows in set (0.00 sec)
mysql> select * from dept;
+----+------+
| id | name |
+----+------+
| 1 | 武当 |
| 2 | 明教 |
| 3 | 少林 |
+----+------+
3 rows in set (0.00 sec)
mysql> select * from emp,dept;
+----+------+------+----+------+
| id | name | dept | id | name |
+----+------+------+----+------+
| 1 | 三丰 | 1 | 1 | 武当 |
| 1 | 三丰 | 1 | 2 | 明教 |
| 1 | 三丰 | 1 | 3 | 少林 |
| 2 | 无忌 | 2 | 1 | 武当 |
| 2 | 无忌 | 2 | 2 | 明教 |
| 2 | 无忌 | 2 | 3 | 少林 |
| 3 | 芷若 | 2 | 1 | 武当 |
| 3 | 芷若 | 2 | 2 | 明教 |
| 3 | 芷若 | 2 | 3 | 少林 |
| 4 | 灭绝 | NULL | 1 | 武当 |
| 4 | 灭绝 | NULL | 2 | 明教 |
| 4 | 灭绝 | NULL | 3 | 少林 |
+----+------+------+----+------+
12 rows in set (0.00 sec)
//多表查询;
mysql> select emp.name,dept.name from emp,dept where emp.dept=dept.id;
+------+------+
| name | name |
+------+------+
| 三丰 | 武当 |
| 无忌 | 明教 |
| 芷若 | 明教 |
+------+------+
//内联 inner join;
mysql> select emp.name,dept.name from emp inner join dept on emp.dept=dept.id;
+------+------+
| name | name |
+------+------+
| 三丰 | 武当 |
| 无忌 | 明教 |
| 芷若 | 明教 |
+------+------+
3.外连接
A:左外连接(left outer join 或 left join)
查询出的结果除了包含符合连接条件的记录以外,还包含左表中剩余的其他记录。
select 表1.列...,表2.列 from 表1 left join 表2 on 连接条件
eg: select stuinfo.name,subject.subjectname from stuinfo left join subject on
stuinfo.subjectid=subject.id;
B: 右外连接(right outer join 或 right join)
查询出的结果除了包含符合连接条件的记录以外,还包含右表中剩余的其他记录。
select 表1.列...,表2.列 from 表1 right join 表2 on 连接条件
字符函数
CONCAT(str1,str2)----连接两个字符串的内容
CONCAT_WS("连接符",str1,str2)---使用连接符来连接字串内容
UPPER(STR) ---将字串转成大写
lOWER(STR)----将所有字串转成小写
LEFT(STR,NUM)---取一个字符串的前多少位
RIGHT(STR,NUM)---取一个字串的后多少位