3

ユーザーが投稿した最後のレコードを取得する必要があります。以下の次のクエリは、グループ化の前に order by を実行できる場合に必要な情報を取得します。

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
4

2 に答える 2

0

sql "partition by " を使用すると、グループ化せずにレコードを並べ替えることができます。

http://learnsqlserver.in/2/Partition-By-Clause.aspx

これは、group by よりも使用するのが最適です。

于 2012-10-23T07:47:19.247 に答える
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

また、「JOIN」キーワードを使用して、where 句の古いスタイルの暗黙的な結合を明示的な結合に変更しました。

于 2012-10-23T08:14:09.403 に答える