1.查询数据

MariaDB [jason]> select * from runoob_tbl limit  offset ;

默认是从偏移量为0 处开始查村数据,通过指定offset 可以从offset 处开始取数

2.where 语句

MariaDB [jason]> select * from runoob_tbl where runoob_title like '%python%';
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
| | 学习python | 菜鸟教程 | -- |
| | 学习python | 菜鸟教程 | -- |
+-----------+--------------+---------------+-----------------+
rows in set (0.00 sec) MariaDB [jason]> select * from runoob_tbl where runoob_title like '%Python%';
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
| | 学习python | 菜鸟教程 | -- |
| | 学习python | 菜鸟教程 | -- |
+-----------+--------------+---------------+-----------------+
rows in set (0.00 sec)

mysql where 条件不区分大小写,若要区分大小写则采用下面的写法

MariaDB [jason]> select * from runoob_tbl where binary runoob_title like '%Python%';
Empty set (0.00 sec) MariaDB [jason]> select * from runoob_tbl where binary runoob_title like '%python%';
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
| | 学习python | 菜鸟教程 | -- |
| | 学习python | 菜鸟教程 | -- |
+-----------+--------------+---------------+-----------------+
rows in set (0.00 sec)

3.update  更新表记录

MariaDB [jason]> update runoob_tbl set runoob_title='java', runoob_author ='jason' where runoob_id = ;
ERROR (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:
Current database: jason Query OK, row affected (0.04 sec)
Rows matched: Changed: Warnings: MariaDB [jason]> select * from runoob_tbl;
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
| | java | jason | -- |
| | 学习scala | 菜鸟教程 | -- |
| | 学习python | 菜鸟教程 | -- |
| | 学习scala | 菜鸟教程 | -- |
+-----------+--------------+---------------+-----------------+

4.删除表记录 delete

delete 语句如果不指定where 条件则会删除表内所有数据

MariaDB [jason]> delete from runoob_tbl where runoob_id = ;
Query OK, row affected (0.00 sec) MariaDB [jason]> select * from runoob_tbl;
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
| | 学习scala | 菜鸟教程 | -- |
| | 学习python | 菜鸟教程 | -- |
| | 学习scala | 菜鸟教程 | -- |
+-----------+--------------+---------------+-----------------+
rows in set (0.00 sec) MariaDB [jason]> delete from runoob_tbl;
Query OK, rows affected (0.00 sec) MariaDB [jason]> select * from runoob_tbl;
Empty set (0.00 sec)

5.like 语句

MariaDB [jason]> select * from runoob_tbl where runoob_title like '%python';
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
| | 学习python | 菜鸟教程 | -- |
+-----------+--------------+---------------+-----------------+

6.union and union all

MariaDB [jason]> select runoob_title as a from runoob_tbl union all select runoob_author as a from runoob_tbl order by a;
+--------------+
| a |
+--------------+
| dong |
| jason |
| 学习java |
| 学习linux |
| 学习python |
| 学习scala |
| 菜鸟教程 |
| 菜鸟教程 |
+--------------+
rows in set (0.00 sec) MariaDB [jason]> select runoob_title from runoob_tbl union all select runoob_author from runoob_tbl;
+--------------+
| runoob_title |
+--------------+
| 学习python |
| 学习scala |
| 学习java |
| 学习linux |
| 菜鸟教程 |
| 菜鸟教程 |
| jason |
| dong |
+--------------+
rows in set (0.00 sec) MariaDB [jason]> select runoob_title from runoob_tbl union select runoob_author from runoob_tbl;
+--------------+
| runoob_title |
+--------------+
| 学习python |
| 学习scala |
| 学习java |
| 学习linux |
| 菜鸟教程 |
| jason |
| dong |
+--------------+
rows in set (0.00 sec) MariaDB [jason]> select runoob_title from runoob_tbl union distinct select runoob_author from runoob_tbl;
+--------------+
| runoob_title |
+--------------+
| 学习python |
| 学习scala |
| 学习java |
| 学习linux |
| 菜鸟教程 |
| jason |
| dong |
+--------------+
rows in set (0.00 sec)

union 与 union distinct 功能相同,会把union 后的结果去重,union all 则不去重。

如果有order 则order 是对全局结果的排序

MariaDB [jason]> select runoob_title from runoob_tbl union all select runoob_author from runoob_tbl order by runoob_author;
ERROR (42S22): Unknown column 'runoob_author' in 'order clause'
MariaDB [jason]> select runoob_title as a from runoob_tbl union all select runoob_author as a from runoob_tbl order by a;
+--------------+
| a |
+--------------+
| dong |
| jason |
| 学习java |
| 学习linux |
| 学习python |
| 学习scala |
| 菜鸟教程 |
| 菜鸟教程 |
+--------------+
rows in set (0.00 sec)

7.排序 order by

MariaDB [jason]> select * from runoob_tbl order by runoob_id asc;
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
| | 学习python | 菜鸟教程 | -- |
| | 学习scala | 菜鸟教程 | -- |
| | 学习java | jason | -- |
| | 学习linux | dong | -- |
+-----------+--------------+---------------+-----------------+
rows in set (0.01 sec) MariaDB [jason]> select * from runoob_tbl order by runoob_id desc;
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
| | 学习linux | dong | -- |
| | 学习java | jason | -- |
| | 学习scala | 菜鸟教程 | -- |
| | 学习python | 菜鸟教程 | -- |
+-----------+--------------+---------------+-----------------+
rows in set (0.00 sec)

8.group by

group by 可以用于sum,count,avg 等聚合函数

首先看一下要操作的表中的数据

MariaDB [jason]> select * from employee_tbl;
+----+--------+---------------------+--------+
| id | name | date | singin |
+----+--------+---------------------+--------+
| | 小明 | -- :: | |
| | 小王 | -- :: | |
| | 小丽 | -- :: | |
| | 小王 | -- :: | |
| | 小明 | -- :: | |
| | 小明 | -- :: | |
+----+--------+---------------------+--------+
rows in set (0.00 sec)
MariaDB [jason]> select name,count(*) from employee_tbl group by name;
+--------+----------+
| name | count(*) |
+--------+----------+
| 小丽 | |
| 小明 | |
| 小王 | |
+--------+----------+
rows in set (0.00 sec)

with rollup 可以实现再分组统计数据的基础之上再进行相同的统计

MariaDB [jason]> select name,sum(singin) from employee_tbl group by name with rollup;
+--------+-------------+
| name | sum(singin) |
+--------+-------------+
| 小丽 | |
| 小明 | |
| 小王 | |
| NULL | |
+--------+-------------+
rows in set (0.00 sec)

但是最后一行的name 为null ,我们可以用coalesce 为其赋值,

coalesce(name,'总数'),如果name 为null 则用 ‘总数’ 来命名

MariaDB [jason]> select coalesce(name,'总数'),sum(singin) from employee_tbl group by name with rollup;
+-------------------------+-------------+
| coalesce(name,'总数') | sum(singin) |
+-------------------------+-------------+
| 小丽 | |
| 小明 | |
| 小王 | |
| 总数 | |
+-------------------------+-------------+
rows in set (0.00 sec)

9.join

MariaDB [jason]> select *  from runoob_tbl a  join employee_tbl  b on a.runoob_id=b.id;
+-----------+--------------+---------------+-----------------+----+--------+---------------------+--------+
| runoob_id | runoob_title | runoob_author | submission_date | id | name | date | singin |
+-----------+--------------+---------------+-----------------+----+--------+---------------------+--------+
| | 学习python | 菜鸟教程 | -- | | 小明 | -- :: | |
| | 学习scala | 菜鸟教程 | -- | | 小王 | -- :: | |
| | 学习java | jason | -- | | 小丽 | -- :: | |
| | 学习linux | dong | -- | | 小王 | -- :: | |
+-----------+--------------+---------------+-----------------+----+--------+---------------------+--------+
rows in set (0.00 sec)
MariaDB [jason]> select *  from runoob_tbl a left join employee_tbl  b on a.runoob_id=b.id;
+-----------+--------------+---------------+-----------------+------+--------+---------------------+--------+
| runoob_id | runoob_title | runoob_author | submission_date | id | name | date | singin |
+-----------+--------------+---------------+-----------------+------+--------+---------------------+--------+
| | 学习python | 菜鸟教程 | -- | | 小明 | -- :: | |
| | 学习scala | 菜鸟教程 | -- | | 小王 | -- :: | |
| | 学习java | jason | -- | | 小丽 | -- :: | |
| | 学习linux | dong | -- | | 小王 | -- :: | |
+-----------+--------------+---------------+-----------------+------+--------+---------------------+--------+
rows in set (0.00 sec)
MariaDB [jason]> select *  from runoob_tbl a right join employee_tbl  b on a.runoob_id=b.id;
+-----------+--------------+---------------+-----------------+----+--------+---------------------+--------+
| runoob_id | runoob_title | runoob_author | submission_date | id | name | date | singin |
+-----------+--------------+---------------+-----------------+----+--------+---------------------+--------+
| | 学习python | 菜鸟教程 | -- | | 小明 | -- :: | |
| | 学习scala | 菜鸟教程 | -- | | 小王 | -- :: | |
| | 学习java | jason | -- | | 小丽 | -- :: | |
| | 学习linux | dong | -- | | 小王 | -- :: | |
| NULL | NULL | NULL | NULL | | 小明 | -- :: | |
| NULL | NULL | NULL | NULL | | 小明 | -- :: | |
+-----------+--------------+---------------+-----------------+----+--------+---------------------+--------+
rows in set (0.00 sec)

10. null 值的处理

mysql 中判断值是否为null 不能用 = null,<> null, 要用 is null  或者is not null, null = null 返回false ,null 与任何值的比较都是false,null 值的比较要用 <=>,null <=> null返回true

11. mysql 中的正则表达式

12.事务

  • 在 MySQL 中只有使用了 Innodb 数据库引擎的数据库或表才支持事务。
  • 事务处理可以用来维护数据库的完整性,保证成批的 SQL 语句要么全部执行,要么全部不执行。
  • 事务用来管理 insert,update,delete 语句
MariaDB [jason]> begin;
Query OK, rows affected (0.00 sec) MariaDB [jason]> insert into runoob_tbl(
-> runoob_title,runoob_author,submission_date
-> )
-> values
-> ("学习php","jason",now()),
-> ("学习c++","dong",now());
Query OK, rows affected, warnings (0.01 sec)
Records: Duplicates: Warnings: MariaDB [jason]> select * from runoob_tbl ;
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
| | 学习python | 菜鸟教程 | -- |
| | 学习scala | 菜鸟教程 | -- |
| | 学习java | jason | -- |
| | 学习linux | dong | -- |
| | 学习php | jason | -- |
| | 学习c++ | dong | -- |
+-----------+--------------+---------------+-----------------+
rows in set (0.00 sec) MariaDB [jason]> rollback;
Query OK, rows affected (0.00 sec) MariaDB [jason]> select * from runoob_tbl ;
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
| | 学习python | 菜鸟教程 | -- |
| | 学习scala | 菜鸟教程 | -- |
| | 学习java | jason | -- |
| | 学习linux | dong | -- |
+-----------+--------------+---------------+-----------------+
rows in set (0.00 sec) MariaDB [jason]> insert into runoob_tbl(
-> runoob_title,runoob_author,submission_date
-> )
-> values
-> ("学习php","jason",now()),
-> ("学习c++","dong",now());
Query OK, rows affected, warnings (0.01 sec)
Records: Duplicates: Warnings: MariaDB [jason]> commit;
Query OK, rows affected (0.00 sec) MariaDB [jason]> select * from runoob_tbl ;
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
| | 学习python | 菜鸟教程 | -- |
| | 学习scala | 菜鸟教程 | -- |
| | 学习java | jason | -- |
| | 学习linux | dong | -- |
| | 学习php | jason | -- |
| | 学习c++ | dong | -- |
+-----------+--------------+---------------+-----------------+
rows in set (0.00 sec)

mysql 基本操作二的更多相关文章

  1. Mysql基本操作、C++Mysql简单应用、PythonMysql简单应用

    MySql基本操作 -- 当指定名称的数据库不存在时创建它并且指定使用的字符集和排序方式 CREATE DATABASE IF NOT EXISTS db_name CHARACTER SET UTF ...

  2. mysql 基本操作语句

    mysql 基本操作笔记: 创建表demo:CREATE TABLE `role` ( `role_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMME ...

  3. css属性编写顺序+mysql基本操作+html细节(个人笔记)

    css属性编写顺序: 影响文档流的属性(比如:display, position, float, clear, visibility, table-layout等) 自身盒模型的属性(比如:width ...

  4. MySQL优化二(连接优化和缓存优化)

    body { font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 1.6; padding-top: 10 ...

  5. 从零开始搭建框架SSM+Redis+Mysql(二)之MAVEN项目搭建

    从零开始搭建框架SSM+Redis+Mysql(二)之MAVEN项目搭建 废话不说,直接撸步骤!!! 1.创建主项目:ncc-parent 选择maven创建项目,注意在创建项目中,packing选择 ...

  6. 【mysql】mysql基本操作

    mysql基本操作 1.mysql表复制 mysql 表结构的复制 create table t2 like t2 mysql 表数据的复制 insert into t2 select * from ...

  7. python/MySQL练习题(二)

    python/MySQL练习题(二) 查询各科成绩前三名的记录:(不考虑成绩并列情况) select score.sid,score.course_id,score.num,T.first_num,T ...

  8. Python/MySQL(二、表操作以及连接)

    Python/MySQL(二.表操作以及连接) mysql表操作: 主键:一个表只能有一个主键.主键可以由多列组成. 外键 :可以进行联合外键,操作. mysql> create table y ...

  9. mysql进阶(二十九)常用函数

    mysql进阶(二十九)常用函数 一.数学函数 ABS(x) 返回x的绝对值 BIN(x) 返回x的二进制(OCT返回八进制,HEX返回十六进制) CEILING(x) 返回大于x的最小整数值 EXP ...

随机推荐

  1. VUE创建播发器组件并调用

    欢迎来到我的友链小屋   首先用vue-cli创建前端项目 参考:https://www.cnblogs.com/ouyangkai/p/11549290.html 新建play.vue文件 编写pl ...

  2. 检测代码潜在bug和质量之SonarQube

    参数使用 项目分析参数可以在多个地方设置,继承关系如下: 全局分析参数,通过Web UI设置,作用于所有项目(配置–>通用–>通用中设置) 项目分析参数,通过WebUI设置,覆盖全局参数( ...

  3. tensorflow常用函数(二)

    一.变量相关的函数 1)tf.train.list_variables(ckpt_dir_or_file)    Returns list of all variables in the checkp ...

  4. 命令行运行ionic run android 出现running one or more of the platforms Error

    问题如图所示: 或者 这个问题也是由于端口号被占用的原因,解决的方法在上篇博客<Ionic run android失败解决方法>.

  5. VUE-v-if和v-else的使用

    1.v-if和v-else <h2 v-if="true">v-if</h2> <h2 v-else>v-else</h2> 注:v ...

  6. VUE--v-on修饰符

    1.v-on的修饰符 .stop:阻止事件冒泡 <div @click="getTitle"> 阿Q <button @click="getBut&qu ...

  7. CDH报错:ScmActive at bootup: Failed to validate the identity of Cloudera Manager.

    报错原因以及解决办法在官网: https://www.cloudera.com/documentation/enterprise/5-8-x/topics/cm_failover_db.html 1. ...

  8. 更改用户host留下的坑

    前言:  我们在创建数据库用户的时候都会指定host,即一个完整的用户可描述为 'username'@'host' .创建用户时不显式指定host则默认为%,%代表所有ip段都可以使用这个用户,我们也 ...

  9. yaml格式配置文件

    YAML 是一种可读性非常高,与程序语言数据结构非常接近.同时具备丰富的表达能力和可扩展性,并且易于使用的数据标记语言. python中处理 Yaml 格式的数据需要先下载pyyaml: pip in ...

  10. PHP随机获取预设的值

    前面我们讲了php怎么获取随机数,<?php echo rand(1000,2000); ?> 一行代码就能搞定,如果要获取ASP,PHP,JAVASCRIPT,AJAX,CSS,JQUE ...