作者yuleen123 (.......................)
看板PHP
标题Re: [请益] MySql select 不同时间的比较
时间Fri Feb 25 21:21:55 2011
※ 引述《sunz5010 (FoFo)》之铭言:
: 我想select一段资料
: 姓名|日期|分数
: -----------------
: 小明|12/1|80
: 大华|12/1|70
: -----------------
: 小明|11/1|70
: 大华|11/1|75
: -----------------
: 小明|10/1|60
: 大华|10/1|80
: 我想找出
: 12/1分数>11/1分数>10/1分数 的人
: 按照上面的数据、他应该会搜寻出小明
: 因为小明(12/1,80)>(11/1,70)>(10/1,60)
: 想请问一下、这样子mysql的语法应该怎麽下呢
我照你的格式建了一张表来测试,如下
mysql> select * from test01;
+------+-------+-------+
| name | date | score |
+------+-------+-------+
| AA | 12/01 | 80 |
| BB | 12/01 | 70 |
| AA | 11/01 | 70 |
| BB | 11/01 | 75 |
| AA | 10/01 | 60 |
| BB | 10/01 | 80 |
+------+-------+-------+
6 rows in set (0.00 sec)
使用以下的 SQL 叙述
select
name
from
test01 as m
where
(select score from test01 as a where date='12/01' and m.name=a.name) >
(select score from test01 as b where date='11/01' and m.name=b.name)
and
(select score from test01 as c where date='11/01' and m.name=c.name) >
(select score from test01 as d where date='10/01' and m.name=d.name)
group by name
结果如下:
+------+
| name |
+------+
| AA |
+------+
1 row in set (0.00 sec)
或许语法不是很高明,不过确实可以达成目的
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 114.41.8.19