高级语句1

关键字排序

使用ORDER BY 语句来实现排序
排序可针对一个或多个字段
ASC:升序,默认排序方式
DESC:降序
ORDER BY的语法节构
SELECT 字段1,字段2… from 表名 order by 字段1,字段2… ASC|DESC

创建库表插入数据
mysql> create database yu;
Query OK, 1 row affected (0.00 sec)
mysql> use yu;
Database changed
mysql> create table heng(id int(4) primary key auto_increment,name varchar(10));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into heng values (1,'zhang'),(2,'li'),,(3,'wang');
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

我要id降序排序

mysql> select * from heng order by id desc;
+----+-------+
| id | name  |
+----+-------+
|  3 | wang  |
|  2 | li    |
|  1 | zhang |
+----+-------+
3 rows in set (0.00 sec)

使用判断语句只要大于1的内容降序排序

mysql> select * from heng where id>1 order by id dessc;
+----+------+
| id | name |
+----+------+
|  3 | wang |
|  2 | li   |
+----+------+
2 rows in set (0.00 sec)

对结果进行分组

使用GROUP BY进行分组
通常结合聚合函数
可以按一个或多个字段对结果进行分组
GROUP BY语法节构
select count(字段),输出组 from 表 判断语句 group by 字段

mysql> select count(id),aihao from chen group by aihhao;
+-----------+-------+
| count(id) | aihao |
+-----------+-------+
|         2 | 1     |
|         3 | 2     |
|         1 | 3     |
+-----------+-------+
3 rows in set (0.00 sec)

将group by和order by结合使用

mysql> select count(id),aihao from chen group by aihao order by id desc;
+-----------+-------+
| count(id) | aihao |
+-----------+-------+
|         1 | 3     |
|         3 | 2     |
|         2 | 1     |
+-----------+-------+
3 rows in set (0.00 sec)

限制结果条目

只返回select查询结果的第一行或前几行
使用limit语句限制条目
limit语句节构
select 字段1,字段2… from 表 limit 偏移量(第一位从0开始);

查看从第三行开始往后三行
mysql> select * from chen limit 2,3;
+----+------+-------+
| id | name | aihao |
+----+------+-------+
|  3 | ai   | 2     |
|  4 | po   | 2     |
|  5 | pp   | 2     |
+----+------+-------+
3 rows in set (0.01 sec)

设置别名

使用as语句设置别名,关键字as可省略
设置别名时,保证不能与库中其他表或字段名称冲突
别名语法结构
select 字段 as 别名名称 from 表;

mysql> select name as 姓名,aihao as 爱好 from chen; 
+--------+--------+
| 姓名   | 爱好   |
+--------+--------+
| wang   | 1      |
| li     | 1      |
| ai     | 2      |
| po     | 2      |
| pp     | 2      |
| ol     | 3      |
+--------+--------+
6 rows in set (0.00 sec)

对表也加入别名

mysql> select a.name as 姓名,a.aihao as 爱好 from chen as a;
+--------+--------+
| 姓名   | 爱好   |
+--------+--------+
| wang   | 1      |
| li     | 1      |
| ai     | 2      |
| po     | 2      |
| pp     | 2      |
| ol     | 3      |
+--------+--------+
6 rows in set (0.00 sec)

as的另一种用法
as作为连接语句
我要创建chen1表需要使用到chen的数据

mysql> create table chen1 as select * from chen;
Query OK, 6 rows affected (0.01 sec)
Records: 6  Duplicates: 0  Warnings: 0
mysql> select * from chen1;
+----+------+-------+
| id | name | aihao |
+----+------+-------+
|  1 | wang | 1     |
|  2 | li   | 1     |
|  3 | ai   | 2     |
|  4 | po   | 2     |
|  5 | pp   | 2     |
|  6 | ol   | 3     |
+----+------+-------+
6 rows in set (0.00 sec)

通配符

用于替换字符串中的部分字符
通常配合like一起使用,并协同where完成查询
常用通配符
%:零个或一个或多个
_:表示单个字符

查询名字里有l的
mysql> select * from chen where name like 'l%';
+----+------+-------+
| id | name | aihao |
+----+------+-------+
|  2 | li   | 1     |
+----+------+-------+
1 row in set (0.00 sec)

子查询

也称为内查询或者嵌套查询
先于主查询被执行,其结果将作为外层主查询的条件
在增删改查中都可以用子查询
支持多层嵌套
in语句时来判断某个值是否在给定的结果集中

语句节构 select * from info where id in 子语句

使用子语句可以做多表相连不见得时左联或者右联
把heng的id当作chen的id使用
mysql> select * from chen where id in (select id from heng);
+----+------+-------+
| id | name | aihao |
+----+------+-------+
|  1 | wang | 1     |
|  2 | li   | 1     |
|  3 | ai   | 2     |
+----+------+-------+
3 rows in set (0.00 sec)

视图

数据库中的虚拟表,这张表中不包含任何数据,只做数据映射,当映射的表中数据变动时跟着变动

创建一个视图表其中做映射的chen表中id大于1的
mysql> create view xxx as select * from chen where id > 1;
Query OK, 0 rows affected (0.01 sec)

NULL值

表示缺失的值
与数字0或空白时不同的
使用ls null 或者ls not null 进行判断
null值和空值区别
空值长度为0,不占用空间;null值长度为null,占用空间
ls null 无法判断空值
空值使用=或<>来处理
count()计算中null忽略,空值加入计算

插入字段
mysql> alter table heng add column xingqu varchar(10);
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0
查看
mysql> select * from heng;
+----+-------+--------+
| id | name  | xingqu |
+----+-------+--------+
|  1 | zhang | NULL   |
|  2 | li    | NULL   |
|  3 | wang  | NULL   |
+----+-------+--------+
3 rows in set (0.00 sec)
统计is null不算记录
mysql> select count(*) from heng where xingqu is null;
+----------+
| count(*) |
+----------+
|        3 |
+----------+
1 row in set (0.00 sec)
插入空值统计空值
mysql> update heng set xingqu='' where name='zhang';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select count(*) from heng where xingqu is not null;
+----------+
| count(*) |
+----------+
|        1 |
+----------+
1 row in set (0.00 sec)

正则表达式

根据指定的匹配模式匹配记录中符合要求的特殊字符
使用regexp关键字指定匹配模式
常用匹配模式
^匹配开始字符
$匹配结束字符
.匹配任意单个字符
*匹配任意一个前面字符
+匹配前面字符至少1次
p1|p2 匹配p1或p2
[…]匹配字符集中任意字符
[^…]匹配不在中括号内任何字符
{n}匹配前面的字符串n次
{n,m}匹配前面字符串至少n次,至多m次

匹配开头不是l的
mysql> select * from chen where name regexp '^[^l]';;
+----+------+-------+
| id | name | aihao |
+----+------+-------+
|  1 | wang | 1     |
|  3 | ai   | 2     |
|  4 | po   | 2     |
|  5 | pp   | 2     |
|  6 | ol   | 3     |
+----+------+-------+
5 rows in set (0.00 sec)

运算符

四种运算

mysql> select 2-2,2+2,7/2,7%2;
+-----+-----+--------+------+
| 2-2 | 2+2 | 7/2    | 7%2  |
+-----+-----+--------+------+
|   0 |   4 | 3.5000 |    1 |
+-----+-----+--------+------+
1 row in set (0.00 sec)
mysql数据库底层进行过优化字符的2不需要进行转换也能比较
如果比较两者有一个是null那就是null
mysql> select 2=4,2='2','r'=null;
+-----+-------+----------+
| 2=4 | 2='2' | 'r'=null |
+-----+------+----------+
|   0 |     1 |     NULL |
+-----+-------+----------+
1 row in set (0.00 sec)


比较大小是比较底层的ASCII码
A:65
a:97
0:48
mysql> select 'a' < 'b';
+-----------+
| 'a' < 'b' |
+-----------+
|         1 |
+-----------+
1 row in set (0.00 sec)

比较是或的关系只要有比前面大的就比前面大
mysql> select 'abc'<'baa';
+-------------+
| 'abc'<'baa' |
+-------------+
|           1 |
+-------------+
1 row in set (0.00 sec)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值