Mysql如何按照日期对比数据
问题描述
-- phpMyAdmin SQL Dump-- version 4.0.10.14-- http://www.phpmyadmin.net---- 主机: localhost:3306-- 生成日期: 2016-08-24 00:16:24-- 服务器版本: 5.6.30-cll-lve-- PHP 版本: 5.6.20SET SQL_MODE = 'NO_AUTO_VALUE_ON_ZERO';SET time_zone = '+00:00';/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;/*!40101 SET NAMES utf8 */;---- 数据库: `student`---- ------------------------------------------------------------ 表的结构 `student`--CREATE TABLE IF NOT EXISTS `student` ( `id` varchar(20) NOT NULL, `name` varchar(50) NOT NULL, `fraction` int(20) NOT NULL, `date` date NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;---- 转存表中的数据 `student`--INSERT INTO `student` (`id`, `name`, `fraction`, `date`) VALUES(’1’, ’student’, 100, ’2016-08-23’),(’1’, ’student’, 95, ’2016-08-22’),(’2’, ’student2’, 100, ’2016-08-23’),(’2’, ’student2’, 200, ’2016-08-22’),(’3’, ’student3’, 350, ’2016-08-23’),(’3’, ’student3’, 450, ’2016-08-22’),(’4’, ’student4’, 50, ’2016-08-23’),(’4’, ’student4’, 100, ’2016-08-22’),(’5’, ’student5’, 900, ’2016-08-23’),(’5’, ’student5’, 930, ’2016-08-22’);/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
MYSQL中的数据
需求:按照下面最终目的对比 昨天和前天的数据(因为每天都要对比最近两天的数据)
需要sql查询语句来实现以下效果
问题解答
回答1:select t1.name as 姓名, t1.fraction as 昨天分数, t2.fraction as 前天分数, current_date as 今天日期from ( select id,name,fraction from student where date = (current_date - interval 1 day) -- 昨天) t1left outer join ( select id,fraction from student where date = (current_date - interval 2 day) -- 前天) t2 on t1.id = t2.id
回答2:select t_22.name, t_22.fraction, t_23.fractionfrom (select name, fraction from student where data=’2016-08-22’) t_22, (select name, fraction from student where data=’2016-08-23’) t_23where t_22.name=t_23.name
有空再看看能不能优化吐槽:题主的id等于没用,id一般用来做主键必然是唯一性的...
相关文章:
1. javascript - 小米浏览器中,图片导致fixed定位的元素无法显示2. Android-studio导入.so库问题?3. javascript - weex和node,js到底是怎样一个关系呢?4. javascript - 微信小程序 如何实现这种左滑动出现删除的办法?有相关api吗?5. node.js - 急急急!node request如何获取响应中cookie的值呢?6. python - 搜索大文件(20G左右)7. angular.js - ng-grid 和tabset一起用时,grid width默认特别小8. 用tp5框架写sql语句9. python - 有哪些预测算法可以根据实时增量数据更新算法并预测后续数据?10. javascript - ajax 图片文件与文本框数据一起提交上传处理
