2012-10-23 84 views
3

我需要获取用户发布的最后一条记录。下面的下面的查询会得到我需要的信息,如果我想,如果我可以按组之前Inner Join(Select。From)

select a.client_id, a.client, b.title, b.type, b.created 
    from profile_users a, node b 
where a.uid = b.uid 
    and b.type = 'event' 
    and a.status=1 
    and a.client_id in (select c.client_id 
         from profile_users c, follows d 
         where c.uid = d.followed_id 
          and d.following_id =3) 
group by a.client_id 
order by a.client_id, 
      b.created desc 

我尝试使用内部联接重写查询,但没有得到期望的结果做订单。我需要编写这个查询,以便在检查下表中的记录后获取client_id。我需要一些帮助来解决这个问题。

select b.client_id, b.client, a.title, a.created 
    from node a 
inner join profile_users b on a.uid=b.uid 
inner join (select c.client_id 
       from profile_users c 
       inner join follows d on c.uid=d.followed_id 
       where c.status = 1 
       and d.following_id = 3 
       order by c.client_id 
      ) as X1 
+0

在第二个内部联接中添加表名 – SRIRAM

+0

@SRIRAM:第二个内部联接不缺少表名,它缺少联接条件。 –

回答

0

您需要,以确定过去后使用联合相关子查询:

select a.client_id, a.client, b.title, b.type, b.created 
    from profile_users a 
    join node b on a.uid = b.uid 
where b.type = 'event' 
    and a.status=1 
    and a.client_id in (select c.client_id 
         from profile_users c 
          join follows d on c.uid = d.followed_id 
         where d.following_id = 3) 
    and b.created = (select max(n2.created) 
        from node n2 
        where n2.uid = a.uid) 
order by a.client_id, 
      b.created desc 

我也改变了旧的风格含蓄的where子句使用“加入”关键字明确那些在加入。

+0

谢谢,修改后的查询有帮助。我仍然在努力,因为输出的是所有记录,而不仅仅是最后一个记录。 – user1767411

+0

以下是对我有效的查询--------从profile_users中选择a.client_id,a.client,b.nid,b.title,b.type,b.created一个加入节点b .uid = b.uid,(选择d.uid,d.client_id,d.client,c.created,c.title from node c join profile_users d on c.uid = d.uid join follow e on d.uid = e.followed_id其中c.type ='event'和d.status = 1且e.following_id = 3按d.client_id排序,c.created desc)n2其中b.type ='event'且a.status = 1且b.uid = n2.uid和b.created = n2.created和b.title = n2.title group by a.client_id – user1767411