1

我有一个查询

SELECT p.*, m.*,
  (SELECT COUNT(*) FROM newPhotoonAlert n WHERE n.userIDfor='$id' AND n.threadID=p.threadID and n.seen='0')  AS unReadCount 
    FROM posts p
    JOIN myMembers m ON m.id = p.user_id
    LEFT JOIN following f 
    ON (p.user_id = f.user_id AND f.follower_id='$id' 
    AND f.request='0' AND f.status='1')
    JOIN myMembers searcher ON searcher.id = '$id' 
    WHERE ((f.follower_id = searcher.id) OR m.id='$id')
    AND p.flagged <'5' 
    ORDER BY p.threadID DESC,p.positionID

它带来了预期的结果,但我想添加另一个 CLAUSE 来限制结果。假设使用上述查询的一组数据样本(显示最少)如下所示。

threadID   postID positionID  url
  564       1254     2         a.com
  564       1245     1         a1.com
  541       1215     3         b1.com 
  541       1212     2         b2.com 
  541       1210     1         b3.com 
  523       745      1         c1.com
  435       689      2         d2.com
  435       688      1         a4.com
  256       345      1         s3.com
  164       316      1         f1.com
  .
  .

我想获得对应于从 MAX 开始的 2 个不同线程 ID 的 ROWS,但我也想包含重复项。

就像是

AND p.threadID IN (Select just Two of all threadIDs currently selected, but include duplicate rows)

所以我的结果应该是

threadID   postID positionID  url
  564       1254     2         a.com
  564       1245     1         a1.com
  541       1215     3         b1.com 
  541       1212     2         b2.com 
  541       1210     1         b3.com 
4

1 回答 1

0

你可以使用这样的东西:

select threadID, postID, positionID, url 
from your_table where 
threadID in
(select * from (select distinct threadID from your_table order by threadID desc limit 10) as tab_alias);

试试这个语法来实现它。

于 2013-10-22T04:20:51.160 回答