Mysql如何按照日期對(duì)比數(shù)據(jù)
問(wèn)題描述
-- phpMyAdmin SQL Dump-- version 4.0.10.14-- http://www.phpmyadmin.net---- 主機(jī): localhost:3306-- 生成日期: 2016-08-24 00:16:24-- 服務(wù)器版本: 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 */;---- 數(shù)據(jù)庫(kù): `student`---- ------------------------------------------------------------ 表的結(jié)構(gòu) `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;---- 轉(zhuǎn)存表中的數(shù)據(jù) `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中的數(shù)據(jù)
需求:按照下面最終目的對(duì)比 昨天和前天的數(shù)據(jù)(因?yàn)槊刻於家獙?duì)比最近兩天的數(shù)據(jù))
需要sql查詢(xún)語(yǔ)句來(lái)實(shí)現(xiàn)以下效果
問(wèn)題解答
回答1:select t1.name as 姓名, t1.fraction as 昨天分?jǐn)?shù), t2.fraction as 前天分?jǐn)?shù), 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
有空再看看能不能優(yōu)化吐槽:題主的id等于沒(méi)用,id一般用來(lái)做主鍵必然是唯一性的...
相關(guān)文章:
1. html5 - ElementUI table中el-table-column怎么設(shè)置百分比顯示。2. python - 使用readlines()方法讀取文件內(nèi)容后,再用for循環(huán)遍歷文件與變量匹配時(shí)出現(xiàn)疑難?3. 對(duì)mysql某個(gè)字段監(jiān)控的功能4. css3 - less或者scss 顏色計(jì)算的知識(shí)應(yīng)該怎么學(xué)?或者在哪里學(xué)?5. 注冊(cè)賬戶(hù)文字不能左右分離6. javascript - table列過(guò)多,有什么插件可以提供列排序和選擇顯示列的功能7. css - 網(wǎng)頁(yè)div區(qū)塊 像蘋(píng)果一樣可左右滑動(dòng) 手機(jī)與電腦8. javascript - 數(shù)組的過(guò)濾和渲染9. html - vue項(xiàng)目中用到了elementUI問(wèn)題10. JavaScript事件
