限制MySQL中返回的记录

时间:2014-02-10 14:39:12

标签: mysql sql

我有两个表之间的一对多关系,我希望能够限制我从一个表中的每个记录的多表中返回的记录数。

关系 tcontributors.CONTRIBUTOR_ID表tclips.FK_CONTRIBUTOR_ID中的一条或多条记录

我知道这应该是直截了当的,它在MS Access中使用“TOP”,但我无法解决SQL for MySQL。任何帮助将不胜感激。

这就是我现在所拥有的......

select *
from tcontributors as qconts
inner join (SELECT * from tclips limit 3) as qclips
where CONTRIBUTOR_ID In (1922,2034,2099) 

3 个答案:

答案 0 :(得分:2)

限制连接中行数的一种方法是进行常规连接,然后运行带有WHERE子句的协调子查询的LIMIT子句,如下所示:

SELECT *
FROM tcontributors AS qconts
INNER JOIN tclips qclips
        ON qclips.fk_contributor_id = qconts.contributor_id
WHERE qconts.contributor_id IN (1922,2034,2099)
  AND qclips.clip_id IN (
    SELECT clip_id
    FROM tclips zclips
    WHERE zclips.fk_contributor_id = qconts.contributor_id
    LIMIT 3
  )

不幸的是,当前版本的MySQL在协调子查询中不支持LIMIT,产生此错误消息(demo on sqlfiddle):

  

此版本的MySQL尚不支持'LIMIT& IN / ALL / ANY / SOME子查询

其他RDBMS引擎确实支持此功能。例如,当修改查询以符合其语法时,Microsoft SQL Server会生成正确的结果:

SELECT *
FROM tcontributors AS qconts
INNER JOIN tclips qclips
        ON qclips.fk_contributor_id = qconts.contributor_id
WHERE qconts.contributor_id IN (1922,2034,2099)
  AND qclips.clip_id IN (
    SELECT TOP 3 clip_id -- Use TOP 3 instead of LIMIT 3
    FROM tclips zclips
    WHERE zclips.fk_contributor_id = qconts.contributor_id
  )

这是working demo on sqlfiddle

答案 1 :(得分:0)

这是答案......

SELECT tcontributors.CONTRIBUTOR_ID, tcontributors.CONTRIBUTOR_NAME,c1.CLIP_ID, c1.CLIP_TITLE
FROM tcontributors
INNER JOIN tclips c1 ON c1.FK_CONTRIBUTOR_ID = tcontributors.CONTRIBUTOR_ID AND c1.FK_CONTRIBUTOR_ID IN (2731,3531,3371)
LEFT JOIN tclips c2 ON c2.FK_CONTRIBUTOR_ID = c1.FK_CONTRIBUTOR_ID AND c2.CLIP_ID < c1.CLIP_ID 
GROUP BY tcontributors.CONTRIBUTOR_ID, tcontributors.CONTRIBUTOR_NAME, c1.CLIP_ID,  c1.CLIP_TITLE 
HAVING (COUNT(c2.CLIP_ID) < 2)
ORDER BY tcontributors.CONTRIBUTOR_ID DESC, c1.CLIP_ID DESC;

答案 2 :(得分:0)

在您的查询中,您没有订购子查询结果,因此您只需要按clip_id限制随机fk_contributor_id,这样我的查询就可以了。如果您需要特定的clip_id,您必须按顺序写下它们。

我认为你可以稍微修改一下这个查询:

<强> SQLFIDDLEExample

SELECT qclips.*,
@row_num := IF(@prev_value=qclips.fk_contributor_id ,@row_num+1,1) AS RowNumber,
@prev_value := qclips.fk_contributor_id 
FROM tcontributors AS qconts
INNER JOIN tclips qclips
        ON qclips.fk_contributor_id = qconts.contributor_id,
      (SELECT @row_num := 1) x,
      (SELECT @prev_value := '') y
WHERE qconts.contributor_id IN (1922,2034,2099)
AND IF(@prev_value=qclips.fk_contributor_id ,@row_num+1,1) < 4
ORDER BY contributor_id

没有行号的结果:

| CLIP_ID | FK_CONTRIBUTOR_ID | CLIP_TITLE |
|---------|-------------------|------------|
|       5 |              1922 |       five |
|       2 |              1922 |        two |
|       3 |              1922 |      three |
|       4 |              1922 |       four |
|       1 |              1922 |        one |
|       6 |              2034 |        six |
|       7 |              2034 |      seven |
|      11 |              2099 |         go |
|       8 |              2099 |        all |
|       9 |              2099 |       good |
|      10 |              2099 |   children |

行号的结果:

| CLIP_ID | FK_CONTRIBUTOR_ID | CLIP_TITLE | ROWNUMBER | PRRW |
|---------|-------------------|------------|-----------|------|
|       2 |              1922 |        two |         2 | 1922 |
|       3 |              1922 |      three |         3 | 1922 |
|       1 |              1922 |        one |         1 | 1922 |
|       6 |              2034 |        six |         1 | 2034 |
|       7 |              2034 |      seven |         2 | 2034 |
|       9 |              2099 |       good |         2 | 2099 |
|      10 |              2099 |   children |         3 | 2099 |
|       8 |              2099 |        all |         1 | 2099 |