日期时间函数
now() 返回服务器当前时间 -----------datetine
curdate() 返回当前日期---------------date
curtime() 返回当前时间---------------time
year(date) 返回指定时间的年份
date(date) 返回指定时间的日期
time(date) 返回指定时间的时间
e.g.
insert into textbook values(2,'神雕侠侣',date('1976/12/5 6:00:00'),now(),year('2019/3/25'));
select * from textbook where 出版时间>'1976-1-1';
时间操作
查找操作
select * from timelog where Date = "2018-07-02";
select * from timelog where Date>="2018-07-01" and Date<="2018-07-31";
日期时间运算
语法格式
select * from 表名 where 字段名 运算符 (时间-interval 时间间隔单位);
时间间隔单位: 1 day | 2 hour | 1 minute | 2 year | 3 month
select * from timelog where shijian > (now()-interval 1 day);
注:
1>时间可以比较大小
2>时间运算 时间-interval 时间间隔
高级查询语句
模糊查询和正则查询
LIKE用于在where子句中进行模糊查询,SQL LIKE 子句中使用百分号 %字符来表示任意字符。
使用 LIKE 子句从数据表中读取数据的通用语法:
SELECT field1, field2,...fieldN
FROM table_name
WHERE field1 LIKE condition1
e.g.
select * from class_1 where name like "A%";
mysql中对正则表达式的支持有限,只支持部分正则元字符
e.g.
select * from class_1 where name regexp 'B.+';
select * from class_1 where name regexp '^.{2}$';
排序
ORDER BY 子句来设定你想按哪个字段哪种方式来进行排序,再返回搜索结果。
使用 ORDER BY 子句将查询数据排序后再返回数据:
SELECT field1, field2,...fieldN table_name1, table_name2...
ORDER BY field1, [field2...] [ASC [DESC]]
默认情况ASC表示升序,DESC表示降序
e.g.
select * from class_1 where sex='m' order by name;
select * from class_1 order by age desc;
select * from class_1 order by age,score;----------复合排序:第一项相同时按照第二项排序
分页
LIMIT 子句用于限制由 SELECT 语句返回的数据数量 或者 UPDATE,DELETE语句的操作数量
带有 LIMIT 子句的 SELECT 语句的基本语法如下:
SELECT column1, column2, columnN
FROM table_name
LIMIT [no of rows]
e.g.
select * from class_1 order by score desc limit 4;