SELECT b.* FROM (SELECT ip,DATE_FORMAT(FROM_UNIXTIME(createtime),"%Y%m%d") AS createtime FROM InApp_AdLog WHERE appid=6 AND bgtype=1 ) AS a, (SELECT ip,DATE_FORMAT(FROM_UNIXTIME(createtime),"%Y%m%d") AS createtime FROM InApp_AdLog WHERE appid=6 AND bgtype=2 ) AS b WHERE a.ip=b.ip GROUP BY b.ip ORDER BY createtime ASC