联盟最后一次登录订购

时间:2015-03-12 11:25:59

标签: mysql union

我试过这个

Select u from users where idU in ((Select idU1 from friends where idU2=1) Union (Select idU2 from friends where idU1=1))

但它不起作用。

所以,我必须这样做:
Select u from users where idU in (Select idU1 from friends where idU2=1) Union Select u from users where idU in (Select idU2 from friends where idU1=1)

但是现在,我想通过上次登录来订购这些用户。

我该怎么办? TY。

2 个答案:

答案 0 :(得分:0)

尝试如下。 我写这篇文章时要记住LastLogin列在用户表

Select 
    u
from
    users
where
    idU in (
            Select idU1 from amistades where idU2 = 1 
            Union 
            Select idU2 from friends where idU1 = 1
            )
ORDER BY LastLogin;

答案 1 :(得分:0)

一旦你的表中有多个行数,你就会想要更快的速度:

SELECT  users.u
    FROM  
      (  SELECT  idU1 AS idU from  amistades where  idU2 = 1
            UNION
         SELECT  idU2 AS idU from  friends where  idU1 = 1 
      ) AS x
    JOIN  users ON x.idU = users.u
    ORDER BY  users.LastLogin;