mysql 基本操作二

1.查询数据

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

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

2.where 语句

MariaDB [jason]> select * from runoob_tbl where runoob_title like ‘%python%‘;
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
|         2 | 学习python   | 菜鸟教程      | 2019-02-10      |
|         4 | 学习python   | 菜鸟教程      | 2019-02-10      |
+-----------+--------------+---------------+-----------------+
2 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 |
+-----------+--------------+---------------+-----------------+
|         2 | 学习python   | 菜鸟教程      | 2019-02-10      |
|         4 | 学习python   | 菜鸟教程      | 2019-02-10      |
+-----------+--------------+---------------+-----------------+
2 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 |
+-----------+--------------+---------------+-----------------+
|         2 | 学习python   | 菜鸟教程      | 2019-02-10      |
|         4 | 学习python   | 菜鸟教程      | 2019-02-10      |
+-----------+--------------+---------------+-----------------+
2 rows in set (0.00 sec)

3.update  更新表记录

MariaDB [jason]> update runoob_tbl set runoob_title=‘java‘, runoob_author =‘jason‘ where runoob_id = 2;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    33
Current database: jason

Query OK, 1 row affected (0.04 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [jason]> select * from runoob_tbl;
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
|         2 | java         | jason         | 2019-02-10      |
|         3 | 学习scala    | 菜鸟教程      | 2019-02-10      |
|         4 | 学习python   | 菜鸟教程      | 2019-02-10      |
|         5 | 学习scala    | 菜鸟教程      | 2019-02-10      |
+-----------+--------------+---------------+-----------------+

4.删除表记录 delete

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

MariaDB [jason]> delete from runoob_tbl where runoob_id = 2;
Query OK, 1 row affected (0.00 sec)

MariaDB [jason]> select * from runoob_tbl;
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
|         3 | 学习scala    | 菜鸟教程      | 2019-02-10      |
|         4 | 学习python   | 菜鸟教程      | 2019-02-10      |
|         5 | 学习scala    | 菜鸟教程      | 2019-02-10      |
+-----------+--------------+---------------+-----------------+
3 rows in set (0.00 sec)

MariaDB [jason]> delete from runoob_tbl;
Query OK, 3 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 |
+-----------+--------------+---------------+-----------------+
|         1 | 学习python   | 菜鸟教程      | 2019-02-11      |
+-----------+--------------+---------------+-----------------+

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    |
| 菜鸟教程     |
| 菜鸟教程     |
+--------------+
8 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         |
+--------------+
8 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         |
+--------------+
7 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         |
+--------------+
7 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 1054 (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    |
| 菜鸟教程     |
| 菜鸟教程     |
+--------------+
8 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 |
+-----------+--------------+---------------+-----------------+
|         1 | 学习python   | 菜鸟教程      | 2019-02-11      |
|         2 | 学习scala    | 菜鸟教程      | 2019-02-11      |
|         3 | 学习java     | jason         | 2019-02-11      |
|         4 | 学习linux    | dong          | 2019-02-11      |
+-----------+--------------+---------------+-----------------+
4 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 |
+-----------+--------------+---------------+-----------------+
|         4 | 学习linux    | dong          | 2019-02-11      |
|         3 | 学习java     | jason         | 2019-02-11      |
|         2 | 学习scala    | 菜鸟教程      | 2019-02-11      |
|         1 | 学习python   | 菜鸟教程      | 2019-02-11      |
+-----------+--------------+---------------+-----------------+
4 rows in set (0.00 sec)

8.group by

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

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

MariaDB [jason]> select * from employee_tbl;
+----+--------+---------------------+--------+
| id | name   | date                | singin |
+----+--------+---------------------+--------+
|  1 | 小明   | 2016-04-22 15:25:33 |      1 |
|  2 | 小王   | 2016-04-20 15:25:47 |      3 |
|  3 | 小丽   | 2016-04-19 15:26:02 |      2 |
|  4 | 小王   | 2016-04-07 15:26:14 |      4 |
|  5 | 小明   | 2016-04-11 15:26:40 |      4 |
|  6 | 小明   | 2016-04-04 15:26:54 |      2 |
+----+--------+---------------------+--------+
6 rows in set (0.00 sec)
MariaDB [jason]> select name,count(*) from employee_tbl group by name;
+--------+----------+
| name   | count(*) |
+--------+----------+
| 小丽   |        1 |
| 小明   |        3 |
| 小王   |        2 |
+--------+----------+
3 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) |
+--------+-------------+
| 小丽   |           2 |
| 小明   |           7 |
| 小王   |           7 |
| NULL   |          16 |
+--------+-------------+
4 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) |
+-------------------------+-------------+
| 小丽                    |           2 |
| 小明                    |           7 |
| 小王                    |           7 |
| 总数                    |          16 |
+-------------------------+-------------+
4 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 |
+-----------+--------------+---------------+-----------------+----+--------+---------------------+--------+
|         1 | 学习python   | 菜鸟教程      | 2019-02-11      |  1 | 小明   | 2016-04-22 15:25:33 |      1 |
|         2 | 学习scala    | 菜鸟教程      | 2019-02-11      |  2 | 小王   | 2016-04-20 15:25:47 |      3 |
|         3 | 学习java     | jason         | 2019-02-11      |  3 | 小丽   | 2016-04-19 15:26:02 |      2 |
|         4 | 学习linux    | dong          | 2019-02-11      |  4 | 小王   | 2016-04-07 15:26:14 |      4 |
+-----------+--------------+---------------+-----------------+----+--------+---------------------+--------+
4 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 |
+-----------+--------------+---------------+-----------------+------+--------+---------------------+--------+
|         1 | 学习python   | 菜鸟教程      | 2019-02-11      |    1 | 小明   | 2016-04-22 15:25:33 |      1 |
|         2 | 学习scala    | 菜鸟教程      | 2019-02-11      |    2 | 小王   | 2016-04-20 15:25:47 |      3 |
|         3 | 学习java     | jason         | 2019-02-11      |    3 | 小丽   | 2016-04-19 15:26:02 |      2 |
|         4 | 学习linux    | dong          | 2019-02-11      |    4 | 小王   | 2016-04-07 15:26:14 |      4 |
+-----------+--------------+---------------+-----------------+------+--------+---------------------+--------+
4 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 |
+-----------+--------------+---------------+-----------------+----+--------+---------------------+--------+
|         1 | 学习python   | 菜鸟教程      | 2019-02-11      |  1 | 小明   | 2016-04-22 15:25:33 |      1 |
|         2 | 学习scala    | 菜鸟教程      | 2019-02-11      |  2 | 小王   | 2016-04-20 15:25:47 |      3 |
|         3 | 学习java     | jason         | 2019-02-11      |  3 | 小丽   | 2016-04-19 15:26:02 |      2 |
|         4 | 学习linux    | dong          | 2019-02-11      |  4 | 小王   | 2016-04-07 15:26:14 |      4 |
|      NULL | NULL         | NULL          | NULL            |  5 | 小明   | 2016-04-11 15:26:40 |      4 |
|      NULL | NULL         | NULL          | NULL            |  6 | 小明   | 2016-04-04 15:26:54 |      2 |
+-----------+--------------+---------------+-----------------+----+--------+---------------------+--------+
6 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.

原文地址:https://www.cnblogs.com/jason-dong/p/10361023.html

时间: 2024-10-09 21:12:38

mysql 基本操作二的相关文章

20141229 mysql基本操作二

修改表 修改表分为两种:修改表自己,修改表内部的字段 修改表自己:修改表名和表选项 修改表名 rename table 表名 to 新表名; 修改表选项 alter table 表名 表选项; 修改存储引擎会修改对应的数据库文件夹下的文件 修改表字段:增删改 alter table 表名 [add/drop/modify/change] 字段名字 数据类型 [位置] 位置:first/ after 字段名,默认的是本身或者所有的字段之后 添加字段 alter table 表名 add [colo

mysql基本操作二

Linux下mysql基本操作

Linux下mysql基本操作                            作者:浩浩哥来了 对mysql进行初始密码的添加 方法(一) mysqladmin -uroot password 123 方法(二) 如果在添加初始密码是报错了可以进行我下面的方法修改密码. ERROR1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) 在/etc/my.cnf文件中添加skip-grant-

95.更改MySQL的root用户密码,MySQL基本操作的常用命令

更改MySQL的root用户密码 1.首次进入数据库 [[email protected] ~]# /usr/local/mysql/bin/mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.36 MySQL Community Server (GPL) Copyright (c) 2000, 2017, O

MySQL基本操作(上)

本博客写是装好Mysql并配好环境变量后的基本操作(windows10系统下)且都是黑框内的操作. 一.登陆MySQL 首先启动服务,在桌面左下角图标处点击右键Windows PowerShell(管理员)(A),然后会出来个蓝框,在蓝框内输入net start mysql80(80是对应的MySQL版本如果是其他版本则输入对应的版本号,如5.7版本输入net start mysql57),然后按回车键,若显示服务已启动,则服务启动成功. 在搜索框内输入cmd则会弹出命令提示符点击进去,所谓的黑

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

MySql基本操作 -- 当指定名称的数据库不存在时创建它并且指定使用的字符集和排序方式 CREATE DATABASE IF NOT EXISTS db_name CHARACTER SET UTF8 COLLATE utf8_general_ci; -- 显示数据库语句 SHOW DATABASES; -- 查看数据库的创建方式,主要是字符类型 SHOW CREATE DATABASE db_name; -- 修改数据库的属性信息,比如修改字符集为 GBK ALTER DATABASE db

【mysql】mysql基本操作

mysql基本操作 1.mysql表复制 mysql 表结构的复制 create table t2 like t2 mysql 表数据的复制 insert into t2 select * from t1 2.mysql索引的操作 2.1索引的增加 alter table tb_name add index index_name (列名) alter table tb_name add unique(列名) alter table tb_name add primary key (列名) 2.2

JAVA使用JDBC连接MySQL数据库 二(2)

本文是对 <JAVA使用JDBC连接MySQL数据库 二>的改进. 上节使用的是PreparedStatement来执行数据库语句,但是preparedStatement需要传递一个sql语句参数,才能创建.然而,DBHelper类只是起到打开和关闭数据库的作用,所以sql语句是要放到应用层部分的,而不是放到DBHelper类中. 而statment不需要传递一个sql语句参数,就能创建. 修改部分如下: public class DBHelper { String driver = &quo

第02章 mysql 基本操作

2015-10-24 目录 参考资料 [1] 唐汉明.深入浅出MySQL 数据库开发.优化与管理维护(第2版)[M].北京:人民邮电出版社,2014 [2] Schwartz.高性能MySQL(第3版)[M].北京:电子工业出版社,2013 [3] DuBois.MySQL Cookbook 中文版 第2版 [M].北京:电子工业出版社,2008 [4] DuBois.MySQL技术内幕(第4版)[M].北京:人民邮电出版社,2011 [5] 范德兰斯.MySQL开发者SQL权威指南 [M].北