1

I have an application that searches and shows user recommendations of different Institutions.

Each Institution can have any number of recommendations.

The results are presented per institute like so:

  • Inst A has 6 recommendations
  • inst B has 4 recommendations
  • and so on...

I want to draw only ten Institutions at a time.

The problem is that my query refers to recommendations, and not to institutions, and therefore I end up getting 10 recommendations instead of 10 institutions.

Is there a way to tell MySQL that the limit refers to DISTINCT institutions, while still pulling in effect DUPLICATE institutions (i.e a number of recommendations on the same institute)?

The simpified versions of the actual query:

SELECT * FROM institutions
LEFT JOIN recommendations
  ON institutions.InstitutionID = recommendations.InstitutionID
WHERE [SOMETHING]
LIMIT 10

a different way to present the question: Is it possible to make the LIMIT clause refer to the original SELECT without the JOIN?

4

3 に答える 3

3

機関に制限を設けてから、そこから参加します。

SELECT * FROM (
    select *
    from institutions
    WHERE [SOMETHING about institutions]
    LIMIT 10) x
LEFT JOIN recommendations ON x.InstitutionID =  recommendations.InstitutionID
    AND [SOMETHING about recommendations]


mysql でのページネーションには、これを使用します。

...
LIMIT 50, 10

この例では、6 ページ目を取得します - 行 51 から開始して 10 行を取得します。
注: 0notからのオフセット カウント1

mysqlの limitの構文LIMIT offset, row_countは. オフセットがなければ、それはLIMIT row_count(私は知っています... row_countパラメータが移動します!)

別の構文を使用することもできます。

...
LIMIT 10 OFFSET 50

この後者の構文は、はるかに読みやすいと思います。

于 2012-11-04T18:08:38.120 に答える
1

参加するときは、機関にサブクエリを作成し、そこに制限を設定します

SELECT * 
  FROM nsrecommendation
  JOIN (select * from nsserviceplace limit 10) as ten_service_places
 WHERE ten_service_places.NSServicePlaceID = nsrecommendation.NSServicePlaceID 
   AND [something];
于 2012-11-04T18:06:23.120 に答える
0

データを取得する方法として mysql クエリのみを使用したい場合は、ネストされたクエリを試して必要なデータを取得できます。テーブル構造なしでは多くを語ることはできません。

于 2012-11-04T18:02:16.700 に答える