數(shù)據(jù)如下:id date fromId toId
--------------------------------------
1 2013-01-01 1 2
2 2013-01-02 2 1
3 2013-01-03 1 3
4 2013-01-04 3 1
5 2013-01-05 4 1
6 2013-01-06 1 4 例如,查詢fromId或toId包含1,去除fromId和toId中數(shù)據(jù)互換的列,僅取日期最大的值,查詢結(jié)果為: id date fromId toId
-------------------------------------- 2 2013-01-02 2 1 4 2013-01-04 3 1 6 2013-01-06 1 4
2 回答

HUX布斯
TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超6個(gè)贊
SELECT max(`date`),maxId,minId FROM (SELECT `date`,IF(fromId>toId,fromId,toId) AS maxId,IF(fromId>toId,toId,fromId) AS minId FROM `table`) AS `tmp` GROUP BY maxId,minId

拉風(fēng)的咖菲貓
TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊
select distinct fromId,* from table group by toId;
添加回答
舉報(bào)
0/150
提交
取消