(一)模糊查询
- 1、-like:
mysql> select name as "姓名" from score where name like '张%';
+--------+
| 姓名 |
+--------+
| 张三 |
| 张玉洁 |
| 张三 |
| 张三 |
| 张三 |
| 张玉洁 |
| 张玉洁 |
| 张玉洁 |
+--------+
8 rows in set
2、is null
mysql> select name as "姓名" from score where name is null;
Empty set
3、between
mysql> select name , score from score where score between 80 and 100;
+--------+-------+
| name | score |
+--------+-------+
| 张玉洁 | 98 |
| 张三 | 86 |
| 张三 | 97 |
| 张玉洁 | 98 |
| 李一 | 80 |
+--------+-------+
5 rows in set
4、in
mysql> select name,score from score where score in (97,98);
+--------+-------+
| name | score |
+--------+-------+
| 张玉洁 | 98 |
| 张三 | 97 |
| 张玉洁 | 98 |
+--------+-------+
3 rows in set
(二)聚合函数
1、sum()
mysql> select sum(score) as "李一总成绩" from score where name="李一";
+------------+
| 李一总成绩 |
+------------+
| 223 |
+------------+
1 row in set
2、avg()
mysql> select avg(score) as "李一平均成绩" from score where name="李一";
+--------------+
| 李一平均成绩 |
+--------------+
| 74.3333 |
+--------------+
1 row in set
3.max() min()
mysql> select max(score) from score;
+------------+
| max(score) |
+------------+
| 98 |
+------------+
1 row in set
4.count()
mysql> select count(score) as "大于70分的成绩个数" from score where score > 70;
+--------------------+
| 大于70分的成绩个数 |
+--------------------+
| 6 |
+--------------------+
1 row in set
(三)分组查询:
mysql> select name,avg(score),courseName from score group by name,courseName;
+--------+------------+------------+
| name | avg(score) | courseName |
+--------+------------+------------+
| 张三 | 97.0000 | 数学 |
| 张三 | 67.0000 | 英语 |
| 张三 | 60.0000 | 语文 |
| 张玉洁 | 43.0000 | 数学 |
| 张玉洁 | 98.0000 | 英语 |
| 张玉洁 | 98.0000 | 语文 |
| 李一 | 80.0000 | 数学 |
| 李一 | 66.0000 | 英语 |
| 李一 | 77.0000 | 语文 |
+--------+------------+------------+
mysql> select name,avg(score),courseName from score group by name,courseName having count(courseName)>1;
+--------+------------+------------+
| name | avg(score) | courseName |
+--------+------------+------------+
| 张三 | 60.0000 | 语文 |
| 张玉洁 | 43.0000 | 数学 |
+--------+------------+------------+
2 rows in set
分组查询总结:
1.WHERE子句从数据源中去掉不符合其搜索条件的数据
2.GROUP BY子句搜集数据行到各个组中
3.统计函数为各个组计算统计值
4.HAVING子句去掉不符合其组搜索条件的各组数据行
5使用GROUP BY时,select后面出现的内容要么为聚合函数,要么为group by后面出现的内容
(四)多表链接查询:
1.内连接:
(1)用inner join 但这种方法不常用 常用第二种;
mysql> select username,starname from users as u inner join star as s on u.starid=s.starid;
+----------+----------+
| username | starname |
+----------+----------+
| 张一 | 白羊座 |
| 李二 | 金牛座 |
| 王三 | 双子座 |
| 张四 | 巨蟹座 |
| 李五 | 狮子座 |
| 张八 | 处女座 |
| 王九 | 天秤座 |
| 张言 | 天蝎座 |
| 李志 | 射手座 |
| 王月 | 水瓶座 |
| 张欣 | 双鱼座 |
+----------+----------+
11 rows in set
(2)from where
select username,starname from users as u,star s where u.starid=s.starid;
+----------+----------+
| username | starname |
+----------+----------+
| 张一 | 白羊座 |
| 李二 | 金牛座 |
| 王三 | 双子座 |
| 张四 | 巨蟹座 |
| 李五 | 狮子座 |
| 张八 | 处女座 |
| 王九 | 天秤座 |
| 张言 | 天蝎座 |
| 李志 | 射手座 |
| 王月 | 水瓶座 |
| 张欣 | 双鱼座 |
+----------+----------+
11 rows in set
2.外连接:
(1)左连接,返回左表的所有数据以及右表对应的数据。 Left join
mysql> select starname,username from users as u left join star s on u.starid=s.starid;
+----------+----------+
| starname | username |
+----------+----------+
| 白羊座 | 张一 |
| 金牛座 | 李二 |
| 双子座 | 王三 |
| 巨蟹座 | 张四 |
| 狮子座 | 李五 |
| 处女座 | 张八 |
| 天秤座 | 王九 |
| 天蝎座 | 张言 |
| 射手座 | 李志 |
| 水瓶座 | 王月 |
| 双鱼座 | 张欣 |
+----------+----------+
11 rows in set
(2)右连接,返回右表的所有数据以及左表相对应的数据。Right join
(五)子查询
在某些特定的业务需求下,当进行查询的时候,需要的条件是另外一个 select 语句的结果,这个时候,就要用到子查询。
用于子查询的关键字主要包括 in、not in、=、!=、exists、not exists ……
1. 比较运算符 > < = !=
mysql> select username,height from users where height >(select height from users where username="张四");
+----------+--------+
| username | height |
+----------+--------+
| 李二 | 155 |
| 王三 | 160 |
| 李五 | 160 |
| 张八 | 168 |
| 王九 | 170 |
| 张言 | 176 |
| 李志 | 180 |
| 王月 | 176 |
| 张欣 | 180 |
+----------+--------+
9 rows in set
2. 子查询 in
mysql> select starid,username,height from users where starid in(select starid from users where height>170);
+--------+----------+--------+
| starid | username | height |
+--------+----------+--------+
| 8 | 张言 | 176 |
| 9 | 李志 | 180 |
| 11 | 王月 | 176 |
| 12 | 张欣 | 180 |
+--------+----------+--------+
4 rows in set
3. Exists
(1)mysql> select starname from star where exists(select * from star where starid=13);
Empty set
(2) mysql> select starname from star where exists(select * from star where starid<13);
+----------+
| starname |
+----------+
| 白羊座 |
| 金牛座 |
| 双子座 |
| 巨蟹座 |
| 狮子座 |
| 处女座 |
| 天秤座 |
| 天蝎座 |
| 射手座 |
| 摩羯座 |
| 水瓶座 |
| 双鱼座 |
+----------+
12 rows in set
Exists()括号内为真 则前面的语句执行,为假则前面的语句不执行;EXISTS也可以作为WHERE 语句的子查询,但一般都能用 IN子查询替换。
4. not exists
与exists刚好是反的。
5. mysql> select sum(avg_score) from(select avg(score) as avg_score from score group by name) as t1;
+----------------+
| sum(avg_score) |
+----------------+
| 215.8333 |
+----------------+
1 row in set
inner join 只返回 匹配项
left join 返回匹配项和左表全项
right join 返回匹配项和右表全项
full join 返回匹配项和两个表的全项
mysqldump -uroot -p1234 db1 > c:\aa.txt 备份
mysqldump -u用户名 -p密码 数据库 < 路径 还原
CRUD:增删改查 create read update delete
插入语句:insert into 表名(字段名)value(相对应的值);
插入多条语句:insert into 表面(字段名)values(相对应的值),(相对应的值);
修改: update 表名 set 字段名=值 where 条件列表;
删除:delete from 表名 where 条件列表;
查询某些字段:select 字段,字段 from 表名 where 条件列表;
1、select userName as "用户名",password “密码” from users;
则返回: 用户名 密码
2、select userName as "用户名",“软院”as “学校名称”from users;
则返回:用户名 学校名称
aa 软院
bb 软院
3、select userName ,"软院" from users;
则返回 : userName 软院
aa 软院
bb 软院
4、select userName from users limit 0,4 ;
(分页 ) 从第一条(下标为0)开始取四条
5、查询排序:
select * from users order by height;//默认为升序
select * from users order by height asc;
select * from users order by height desc;
1、concat(s1,s2,s3,...,sn)连接字符串;
2、insert(str,pos , len ,newstr);
如:insert(“abcd”,1,2,“xp” ); xpcd
3、lower(str) lcase( str ) 把str所有字符变成小写;
upper(str)ucase(str) 把str所有字符变成大写;
4、left ( str,len)
如:left(abcd,3); abc
5、right(str,len); bcd
6、lpad(str,len,padstr)
如:mysql>select lpad("abc",5,"***"); **abc// 长度大于字符串长度 则补入*补成len长度的字符串
mysql>select lpad("abc",2,"**"); ab //长度小于字符串长度,则从左裁剪等长
7、rpad(str,len,padstr)
8、replace(str ,from_str,to_str)
如:mysql> select replace("abcd","ab","guanxin");
+--------------------------------+
| replace("abcd","ab","guanxin") |
+--------------------------------+
| guanxincd |
+--------------------------------+
9、trim(str) //去掉字符串前后空格
10、substring(str,pos,len)
如:mysql> select substring("abcdef",1,5);
+-------------------------+
| substring("abcdef",1,5) |
+-------------------------+
| abcde |
+-------------------------+
1、abs(x) //返回x的绝对值
如:mysql> select abs(-3.14);
+------------+
| abs(-3.14) |
+------------+
| 3.14 |
+------------+
2、rand()//返回0-1之间的随机数
如:mysql> select rand();
+---------------------+
| rand() |
+---------------------+
| 0.38327023888311373 |
+---------------------+
1 row in set
mysql> select rand();
+--------------------+
| rand() |
+--------------------+
| 0.6705344940261305 |
+--------------------+
1 row in set
Rand(x);//返回为一样的0-1之间的随机数
如:mysql> select rand(1);
+---------------------+
| rand(1) |
+---------------------+
| 0.40540353712197724 |
+---------------------+
1 row in set
mysql> select rand(1);
+---------------------+
| rand(1) |
+---------------------+
| 0.40540353712197724 |
+---------------------+
1 row in set
mysql> select rand(11);
+-------------------+
| rand(11) |
+-------------------+
| 0.907234631392392 |
+-------------------+
1 row in set
3、truncate(x,y)//x:小数 y:取小数点几位
如:mysql> select truncate(3.14566,3);
+---------------------+
| truncate(3.14566,3) |
+---------------------+
| 3.145 |
+---------------------+
1 row in set
mysql> select truncate(3.14566,0);
+---------------------+
| truncate(3.14566,0) |
+---------------------+
| 3 |
+---------------------+
1 row in set
1、curdate() //当前日期
如:mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2013-09-23 |
+------------+
1 row in set
2、curtime()//当前时间
如:mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 22:54:52 |
+-----------+
1 row in set
3、now()、 localtime()、localtime、sysdate()、
如:返回的都是:
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2013-09-23 22:56:27 |
+---------------------+
1 row in set
mysql> select localtime();
+---------------------+
| localtime() |
+---------------------+
| 2013-09-23 22:56:54 |
+---------------------+
1 row in set
mysql> select localtime;
+---------------------+
| localtime |
+---------------------+
| 2013-09-23 22:57:26 |
+---------------------+
1 row in set
mysql> select sysdate();
+---------------------+
| sysdate() |
+---------------------+
| 2013-09-23 22:58:04 |
+---------------------+
1 row in set
4、year(date)//返回年份
如:mysql> select year(now());
+-------------+
| year(now()) |
+-------------+
| 2013 |
+-------------+
1 row in set
mysql> select(localtime());
+---------------------+
| (localtime()) |
+---------------------+
| 2013-09-23 22:59:40 |
+---------------------+
1 row in set
5、month(date)//返回月份
如:mysql> select month(localtime);
+------------------+
| month(localtime) |
+------------------+
| 9 |
+------------------+
1 row in set
mysql> select month(sysdate());
+------------------+
| month(sysdate()) |
+------------------+
| 9 |
+------------------+
1 row in set
6、monthname(date)//返回月份的英文名
如:mysql> select monthname(now());
+------------------+
| monthname(now()) |
+------------------+
| September |
+------------------+
1 row in set
1、if(expr1,expr2,expr3);expr1如果为true 则返回expr2的值,如果为false 则返回expr3的值
mysql> select if(age=20,100,age) from student;
+--------------------+
| if(age=20,100,age) |
+--------------------+
| 21 |
| 22 |
| 100 |
| 28 |
| 26 |
+--------------------+
5 rows in se
2、ifnull(expr1,expr2) 如果expr1不为空,则返回expr1的值,如果expr1为空则返回expr2的值
mysql> select userName from student;
+----------+
| userName |
+----------+
| NULL |
| 李四 |
| 张海天 |
| 张欣 |
| 张传 |
+----------+
5 rows in set
mysql> select ifnull(userName,"空");
mysql> select ifnull(userName,"空")from student;
+-----------------------+
| ifnull(userName,"空") |
+-----------------------+
| 空 |
| 李四 |
| 张海天 |
| 张欣 |
| 张传 |
+-----------------------+
5 rows in set
3、case when(value) then (result) else (defailt) end
mysql> select age from student;
+-----+
| age |
+-----+
| 21 |
| 22 |
| 20 |
| 28 |
| 26 |
+-----+
5 rows in set
mysql> select case when age>27 then "最大" else age end
from student;
+-------------------------------------------+
| case when age>27 then "最大" else age end |
+-------------------------------------------+
| 21 |
| 22 |
| 20 |
| 最大 |
| 26 |
+-------------------------------------------+
5 rows in set
4、case (expr) when (value) then (result) end
mysql> select age from student;
+-----+
| age |
+-----+
| 21 |
| 22 |
| 20 |
| 28 |
| 26 |
+-----+
5 rows in set
mysql> select case age when 28 then "最大" when 20 then "最小" else age end from student;
+---------------------------------------------------------------+
| case age when 28 then "最大" when 20 then "最小" else age end |
+---------------------------------------------------------------+
| 21 |
| 22 |
| 最小 |
| 最大 |
| 26 |
+---------------------------------------------------------------+
5 rows in set
1、返回当前数据库名称:
mysql> select database();
+--------------+
| database() |
+--------------+
| student_test |
+--------------+
1 row in set
2、返回当前数据库版本信息
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.5.25 |
+-----------+
1 row in set
3、返回当前登录用户信息:
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set
4、加密后返回字符串
mysql> select password(userNumber) from student where userId=1;
+-------------------------------------------+
| password(userNumber) |
+-------------------------------------------+
| *6FECC218350DB1E056A9D5DC8A42CD3948AFEB6D |
+-------------------------------------------+
1 row in set
mysql> select md5(userNumber) from student where userId=1;
+----------------------------------+
| md5(userNumber) |
+----------------------------------+
| 3785ef83d0f3ea3541f3a1cb49f75b0e |
+----------------------------------+
1 row in set