限制 - mysql 查询

发布于 2024-12-26 03:59:40 字数 616 浏览 0 评论 0原文

我想知道如何通过 id_offers 数量而不是行数来限制输出。例如

 SELECT  A.id_offer, T.tags  
            FROM offer A
            INNER JOIN offer_has_tags Z
            ON A.id_offer = Z.offer_id_offer

            INNER JOIN tags T
            ON Z.tags_id_tags = T.id_tags

            WHERE state = 0 
            ORDER BY date
            DESC LIMIT 0, 10

输出:

  id_offer  tags
    77       xx 
    76       xx 
    76       xx 
    75       xx 
    75       xx 
    74       xx 
    74       xx
    73       xx
    73       xx
    72       xx

编辑:在这种情况下,仅应算作 6 个报价。

I would like to know how can i limit the output by the number of id_offers and not the number of rows. For example

 SELECT  A.id_offer, T.tags  
            FROM offer A
            INNER JOIN offer_has_tags Z
            ON A.id_offer = Z.offer_id_offer

            INNER JOIN tags T
            ON Z.tags_id_tags = T.id_tags

            WHERE state = 0 
            ORDER BY date
            DESC LIMIT 0, 10

output:

  id_offer  tags
    77       xx 
    76       xx 
    76       xx 
    75       xx 
    75       xx 
    74       xx 
    74       xx
    73       xx
    73       xx
    72       xx

Edit: In this case only should be count as 6 offers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

权谋诡计 2025-01-02 03:59:40

我不确定这是否正是您想要的,但我认为是:

SELECT  A.id_offer, T.tags  
FROM offer A
JOIN offer_has_tags Z
  ON A.id_offer = Z.offer_id_offer
JOIN tags T
  ON Z.tags_id_tags = T.id_tags
JOIN (
    SELECT DISTINCT id_offer
    FROM offer
    WHERE state = 0
    ORDER BY date DESC
    LIMIT 10
) L
  ON A.id_offer = L.id_offer

或更简单:

SELECT  A.id_offer, T.tags  
FROM 
  ( SELECT *
    FROM offer
    WHERE state = 0
    ORDER BY date DESC
    LIMIT 10
  ) A
JOIN offer_has_tags Z
  ON A.id_offer = Z.offer_id_offer
JOIN tags T
  ON Z.tags_id_tags = T.id_tags

I'm not sure if this is exactly what you want, but I think it is:

SELECT  A.id_offer, T.tags  
FROM offer A
JOIN offer_has_tags Z
  ON A.id_offer = Z.offer_id_offer
JOIN tags T
  ON Z.tags_id_tags = T.id_tags
JOIN (
    SELECT DISTINCT id_offer
    FROM offer
    WHERE state = 0
    ORDER BY date DESC
    LIMIT 10
) L
  ON A.id_offer = L.id_offer

or the more simple:

SELECT  A.id_offer, T.tags  
FROM 
  ( SELECT *
    FROM offer
    WHERE state = 0
    ORDER BY date DESC
    LIMIT 10
  ) A
JOIN offer_has_tags Z
  ON A.id_offer = Z.offer_id_offer
JOIN tags T
  ON Z.tags_id_tags = T.id_tags
话少心凉 2025-01-02 03:59:40

你可以试试这个:

SELECT  A.id_offer, T.tags  
FROM offer A
    INNER JOIN offer_has_tags Z
        ON A.id_offer = Z.offer_id_offer
    INNER JOIN tags T
        ON Z.tags_id_tags = T.id_tags

WHERE (state = 0) AND
      (A.id_offer >= 72 AND A.id_offer <= 77)
ORDER BY date

you can try this:

SELECT  A.id_offer, T.tags  
FROM offer A
    INNER JOIN offer_has_tags Z
        ON A.id_offer = Z.offer_id_offer
    INNER JOIN tags T
        ON Z.tags_id_tags = T.id_tags

WHERE (state = 0) AND
      (A.id_offer >= 72 AND A.id_offer <= 77)
ORDER BY date
友欢 2025-01-02 03:59:40

您只需使用 DISTINCT:

SELECT DISTINCT A.id_offer, T.tags
来自报价 A
内连接offer_has_tags Z
ON A.id_offer = Z.offer_id_offer

        INNER JOIN tags T
        ON Z.tags_id_tags = T.id_tags

        WHERE state = 0 
        ORDER BY date
        DESC LIMIT 0, 10

You simply have to use DISTINCT:

SELECT DISTINCT A.id_offer, T.tags
FROM offer A
INNER JOIN offer_has_tags Z
ON A.id_offer = Z.offer_id_offer

        INNER JOIN tags T
        ON Z.tags_id_tags = T.id_tags

        WHERE state = 0 
        ORDER BY date
        DESC LIMIT 0, 10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文