1.查看mysql版本, 当前用户和当前的时间(不区分大小写)
mysql> select user(),version(),currenT_date,now(),sysDate(),CURDATE(); +----------------+-------------------------+--------------+---------------------+---------------------+------------+ | user() | version() | currenT_date | now() | sysDate() | CURDATE() | +----------------+-------------------------+--------------+---------------------+---------------------+------------+ | root@localhost | 5.5.38-0ubuntu0.14.04.1 | 2014-10-27 | 2014-10-27 15:22:48 | 2014-10-27 15:22:48 | 2014-10-27 | +----------------+-------------------------+--------------+---------------------+---------------------+------------+ 1 row in set (0.00 sec)2.mysql可以作为一个简单的计算器(a simple calculator )
mysql> select cos(PI()/4),(4+2)/3 As basicOperation; +--------------------+----------------+ | cos(PI()/4) | basicOperation | +--------------------+----------------+ | 0.7071067811865476 | 2.0000 | +--------------------+----------------+ 1 row in set (0.00 sec)3.数据库的创建和使用
mysql> CREATE DATABASE mytest; Query OK, 1 row affected (0.02 sec) //使用数据库 mysql> use mytest; Database changed //删除数据库 mysql> drop database mytest; Query OK, 0 rows affected (0.17 sec)每次登陆都必须选择创建的数据库来开启一次mysql会话,可以使用下面的方式直接指定使用的数据库
user@user-xubuntu:~$ mysql -h 127.0.0.1 -u root -p newtest Enter password:-h 指定本机IP或者是localhost或者是127.0.0.1;
4.表的创建和使用
mysql> create table person( -> name varchar(30), -> password varchar(32), -> email varchar(50), -> age int, -> createTime date, -> deleteTime date -> ); Query OK, 0 rows affected (0.12 sec) mysql> show tables; +-------------------+ | Tables_in_newtest | +-------------------+ | person | | user | +-------------------+ 2 rows in set (0.00 sec) mysql> desc person; +------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+-------------+------+-----+---------+-------+ | name | varchar(30) | YES | | NULL | | | password | varchar(32) | YES | | NULL | | | email | varchar(50) | YES | | NULL | | | age | int(11) | YES | | NULL | | | createTime | date | YES | | NULL | | | deleteTime | date | YES | | NULL | | +------------+-------------+------+-----+---------+-------+ 6 rows in set (0.00 sec)5.往空数据表中添加数据的两种方式
mysql> LOAD DATA LOCAL INFILE '/{path}/people.txt' INTO TABLE person //mysql 5.7的版本支持 <pre class="programlisting"> //If you created the file on Windows with an editor that uses <code class="literal">\r\n</code> as a line terminator, you should use this statement instead: mysql> LOAD DATA LOCAL INFILE '/{path}/people.txt' INTO TABLE person -> LINES TERMINATED BY '\r\n';
mysql> source /home/user/Desktop/newFile/person.sql; //使用Source的方式导入.sql文件
6.表的创建和简单查询操作以及对时间的操作
//基本的模版 SELECT what_to_select FROM which_table WHERE conditions_to_satisfy;
基本操作:插入语句, 搜索所有的数据,根据条件搜索特定的行,根据条件搜索特定的列。
mysql> insert into person values('evan','123456','[email protected]',21); Query OK, 1 row affected (0.03 sec) //搜索所有的数据 mysql> select * from person; +------+----------+-----------------+------+------------+------------+ | name | password | email | age | createTime | deleteTime | +------+----------+-----------------+------+------------+------------+ | evan | 123456 | [email protected] | 21 | 2010-01-01 | 2010-10-10 | | ovan | 123456 | [email protected] | 23 | 2012-03-15 | NULL | | kvan | 123456 | [email protected] | 22 | 2012-03-18 | NULL | | kvan | 123456 | [email protected] | 18 | 2012-02-15 | NULL | | kvan | 123456 | [email protected] | 26 | 2012-04-18 | NULL | +------+----------+-----------------+------+------------+------------+ 5 rows in set (0.00 sec) //根据条件搜索特定的行 mysql> select * from person where name = 'evan'; +------+----------+-----------------+------+------------+------------+ | name | password | email | age | createTime | deleteTime | +------+----------+-----------------+------+------------+------------+ | evan | 123456 | [email protected] | 21 | 2010-01-01 | 2010-10-10 | +------+----------+-----------------+------+------------+------------+ 1 row in set (0.00 sec) //根据条件搜索特定的列 mysql> select name,password from person where name = 'evan'; +------+----------+ | name | password | +------+----------+ | evan | 123456 | +------+----------+ 1 row in set (0.00 sec) //对搜索记录进行降序排序,也可对多列进行排序,desc只对它最近的那个列名起作用 mysql> select * from person order by age desc; +------+----------+-----------------+------+------------+------------+ | name | password | email | age | createTime | deleteTime | +------+----------+-----------------+------+------------+------------+ | kvan | 123456 | [email protected] | 26 | 2012-04-18 | NULL | | ovan | 123456 | [email protected] | 23 | 2012-03-15 | NULL | | kvan | 123456 | [email protected] | 22 | 2012-03-18 | NULL | | evan | 123456 | [email protected] | 21 | 2010-01-01 | 2010-10-10 | | kvan | 123456 | [email protected] | 18 | 2012-02-15 | NULL | +------+----------+-----------------+------+------------+------------+ 5 rows in set (0.00 sec) //CURDATE(获取当前的时间),返回当前时间;
//TIMESTAMPDIFF(xxx,xxx,xxx)可以用来计算两个时间段的年/月/日等的天数差,比如计算Q龄。还可以对下面的durationTime进行排序! mysql> select name,age,createTime,CURDATE(),TIMESTAMPDIFF(YEAR,createTime,CURDATE()) AS durationTime from person order by age; +------+------+------------+------------+--------------+ | name | age | createTime | CURDATE() | durationTime | +------+------+------------+------------+--------------+ | kvan | 18 | 2012-02-15 | 2014-10-27 | 2 | | evan | 21 | 2010-01-01 | 2014-10-27 | 4 | | kvan | 22 | 2012-03-18 | 2014-10-27 | 2 | | ovan | 23 | 2012-03-15 | 2014-10-27 | 2 | | kvan | 26 | 2012-04-18 | 2014-10-27 | 2 | +------+------+------------+------------+--------------+ 5 rows in set (0.00 sec) //month函数的使用,获取指定时间的月份; MONTH()函数返回的数字是1-12;
//MOD(MONTH(CURDATE()),12) 返回的数据是0-11; mysql> select name,age,createTime from person where month(createTime)=2; +------+------+------------+ | name | age | createTime | +------+------+------------+ | kvan | 18 | 2012-02-15 | +------+------+------------+ 1 row in set (0.01 sec) //还有很多关于操作时间的函数,再次不一一赘述,可查找官网了解更多。
7.mysql对空值的操作以及mysql的模糊匹配
mysql> select 1 is null,1 is not null, 0 is null, 0 is not null, '' is null, '' is not null, 1 = null, 1 <> null, 1 > null, 1 < null; +------------+---------------+-----------+---------------+------------+----------------+----------+-----------+----------+----------+ | 1 is null | 1 is not null | 0 is null | 0 is not null | '' is null | '' is not null | 1 = null | 1 <> null | 1 > null | 1 < null | +------------+---------------+-----------+---------------+------------+----------------+----------+-----------+----------+----------+ | 0 | 1 | 0 | 1 | 0 | 1 | NULL | NULL | NULL | NULL | +------------+---------------+-----------+---------------+------------+----------------+----------+-----------+----------+----------+ 1 row in set (0.00 sec)可以看出NULL意味着“失踪的未知的值”,在mysql中,
0
or NULL
意味着 false ,anything else 意味着 true;SQL模式匹配中,使用“_”匹配任何单个字符“%”匹配任意数量的字符(包括零个字符)
mysql> select * from person where name LIKE '__p%m%q__'; //表示,匹配任意两个单字符开头,第三个字符是p,倒数第三个字符是q,最后两个字符是任意两个单字符,字符p和字符q之间包含字符m的任意符合条件的字符串。
使用REGEXP和not REGEXP 进行sql匹配:
mysql> select * from person where name REGEXP '^.{4}$'; +------+----------+-----------------+------+------------+------------+ | name | password | email | age | createTime | deleteTime | +------+----------+-----------------+------+------------+------------+ | evan | 123456 | [email protected] | 21 | 2010-01-01 | 2010-10-10 | | ovan | 123456 | [email protected] | 23 | 2012-03-15 | NULL | | kvan | 123456 | [email protected] | 18 | 2012-02-15 | NULL | | kvan | 123456 | [email protected] | 26 | 2012-04-18 | NULL | +------+----------+-----------------+------+------------+------------+ 4 rows in set (0.00 sec) mysql> select * from person where name REGEXP '^....$';//和上面的查询语句是等价的 +------+----------+-----------------+------+------------+------------+ | name | password | email | age | createTime | deleteTime | +------+----------+-----------------+------+------------+------------+ | evan | 123456 | [email protected] | 21 | 2010-01-01 | 2010-10-10 | | ovan | 123456 | [email protected] | 23 | 2012-03-15 | NULL | | kvan | 123456 | [email protected] | 18 | 2012-02-15 | NULL | | kvan | 123456 | [email protected] | 26 | 2012-04-18 | NULL | +------+----------+-----------------+------+------------+------------+ 4 rows in set (0.00 sec)8. mysql的count()函数的使用
mysql> select count(*) from person; +----------+ | count(*) | +----------+ | 5 | +----------+ 1 row in set (0.43 sec)
count(*)是对行数目进行计数。
count(item),其中item表示列名,count(item)表示对列中不为空的行进行计数。
在不加where条件限制的情况下,COUNT(*)与COUNT(COL)基本可以认为是等价的;但是在有Where条件限制的情况下,count(*)会比count(col)快非常多。
网上关于count的优化策略,对于MYISAM表来说:
1.任何情况下SELECT COUNT(*) FROM tablename是最优选择;
2.尽量减少SELECT COUNT(*) FROM tablename WHERE COL = 'value' 这种查询;
3.杜绝SELECT COUNT(COL) FROM tablename WHERE COL2 = 'value' 的出现。
mysql> select name,age,count(*) from person; +------+------+----------+ | name | age | count(*) | +------+------+----------+ | evan | 21 | 5 | +------+------+----------+ 1 row in set (0.00 sec) mysql> set SQL_MODE = "ONLY_FULL_GROUP_BY"; Query OK, 0 rows affected (0.06 sec) mysql> select name,age,count(*) from person; ERROR 1140 (42000): Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause mysql> set sql_mode = ""; Query OK, 0 rows affected (0.00 sec) mysql> select name,age,count(*) from person; +------+------+----------+ | name | age | count(*) | +------+------+----------+ | evan | 21 | 5 | +------+------+----------+ 1 row in set (0.00 sec) //默认sql_mode为空,select name,age,count(*) from person;这个查询即使不加group by子句也不会报错,但是当我们设置sql_mode的模式之后,这个查询会报错。很明显需要加上group by name,age子句 //有人这样实现sql语句,在函数中添加一个三目运算作为参数判断是否为空。 SELECT owner, COUNT(*) AS total_pets, COUNT(IF(dead = '0', 1, NULL)) AS alive_pets, COUNT(IF(dead = '1', 1, NULL)) AS dead_pets FROM pet GROUP BY owner;