看板 PHP 關於我們 聯絡資訊
※ 引述《yuleen123 (.......................)》之銘言: : ※ 引述《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) : 或許語法不是很高明,不過確實可以達成目的 我剛剛想了一下, 如果是要確認某個人是不是都一直進步, 那麼多加一個差值的欄位(每一次插入的時候跟上一次比較)是不是會有效率多了? 因為感覺上如果日期一多, 人一多, 暴力法一一比對很恐怖... +------+-------+-------+------+ | name | date | score | diff | +------+-------+-------+------+ | AA | 12/01 | 80 | 10 | | BB | 12/01 | 70 | -5 | | AA | 11/01 | 70 | 10 | | BB | 11/01 | 75 | -5 | | AA | 10/01 | 60 | 0 | | BB | 10/01 | 80 | 0 | +------+-------+-------+------+ 如果多了一個差值欄位, 那就只要select所有的人當中diff沒有負值(也就是都>=0) 就會是所選的人了... 不知道這樣的做法有沒有其他瑕疵我沒考慮到的, 不過不能加欄位的話當我沒說, 也希望有前輩可以分享一下作法。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 111.248.106.146
arrack:這只能比較出一天的差異,原PO想要多天的 02/25 23:22
andreli:我有說前提...如果原po是想找成績一直往上的或一直往下的 02/25 23:24
andreli:所以我的方法所以還是要看原po的真正case 02/25 23:27