propertyid userid shareddatetime
32 129 2015-02-03 11:09:33
33 129 2015-02-03 11:28:20
30 128 2015-02-03 11:33:02
32 128 2015-02-02 13:30:44
33 127 2015-02-02 13:40:44
30 128 2015-02-03 11:31:54
以上结果来自查询:
SELECT propertyid,userid,shareddatetime FROM `groupsharing` gs WHERE gs.groupid IN( SELECT `groupnum` FROM `groupuser` WHERE user = 129 ) AND gs.shareddatetime = (SELECT MIN(shareddatetime) FROM groupsharing gs2 WHERE gs.propertyid = gs2.propertyid )
UNION
SELECT property,user,shareddatetime FROM sharing WHERE `shareduser` = '129' and `shareddatetime` = ( select min( `shareddatetime` ) from sharing s2 where s2.`shareduser` = sharing.`shareduser` and s2.`property` = sharing.`property` )
现在,如何从上述结果中选择具有最小共享日期时间的propertyid?
我的预期结果如下:
propertyid userid shareddatetime
32 128 2015-02-02 13:30:44
33 127 2015-02-02 13:40:44
30 128 2015-02-03 11:31:54
提前致谢
答案 0 :(得分:0)
你可以试试这个:
Select a.propertyid, a.userid, min(a.shareddatetime)
from (SELECT propertyid,userid,shareddatetime
FROM `groupsharing` gs WHERE gs.groupid IN(SELECT `groupnum` FROM `groupuser` WHERE user = 129 ) AND gs.shareddatetime = (SELECT MIN(shareddatetime) FROM groupsharing gs2 WHERE gs.propertyid = gs2.propertyid )
UNION
SELECT property,user,shareddatetime
FROM sharing WHERE `shareduser` = '129' and `shareddatetime` = ( select min( `shareddatetime` ) from sharing s2 where s2.`shareduser` = sharing.`shareduser` and s2.`property` = sharing.`property` )
) as a group by a.propertyid, a.userid;
答案 1 :(得分:-1)
上述答案如下:
SELECT propertyid,userid,shareddatetime FROM(SELECT propertyid,userid,shareddatetime FROM groupsharing
gs WHERE gs.groupid IN(SELECT groupnum
FROM groupuser
WHERE user = 129)AND gs。 shareddatetime =(SELECT MIN(shareddatetime)FROM groupsharing gs2 WHERE gs.propertyid = gs2.propertyid)UNION SELECT属性,user,shareddatetime FROM sharing WHERE shareduser
=' 129' AND shareddatetime
=(SELECT min(shareddatetime
)FROM sharing s2 WHERE s2。shareduser
= sharing。shareduser
AND s2。property
= sharing。property
))u
在哪里。shareddatetime
=(SELECT MIN(shareddatetime)from(SELECT propertyid,userid,shareddatetime FROM groupsharing
gs WHERE gs.groupid IN(SELECT groupnum
FROM groupuser
WHERE user = 129)AND gs.shareddatetime =(SELECT MIN(shareddatetime)FROM groupsharing gs2 WHERE gs.propertyid = gs2.propertyid)UNION SELECT属性,user,shareddatetime FROM sharing WHERE shareduser
=' 129' AND shareddatetime
=(SELECT min(shareddatetime
)FROM sharing s2 WHERE s2。shareduser
= sharing。shareduser
AND s2。property
= sharing。{{1 }))ui WHERE u.propertyid = ui.propertyid)