JOINed mySQL 查询和 LIMIT

发布于 2025-01-06 03:15:22 字数 628 浏览 1 评论 0原文

这是我在这个论坛上的第一篇文章(所以请温柔一点)

我正在研究一个巨大的产品数据库,并且我有很多关于它们的信息表。

有一个表如下所示:

product

  • Product_id
  • name
  • Brand

另一个表包含同一产品的不同类型,如下所示:

product_type

  • type_id
  • type
  • cost
  • Product_id

I使用联接来选择两个表,它工作正常,但由于两个表中有一千多个条目,我想限制选择并在页面上仅显示 15 个条目。

所以我的问题来了:

我选择连接的两个表并将它们限制为 15。但是由于类型比产品多,我选择了属于未知数量产品的 15 个产品类型(因为一个产品可以有 1、2 或任何数量)类型)。我需要能够在两个表中进行搜索,并根据两个表中的列对结果进行排序,仅从产品中选择 15 个条目并从每个产品类型中选择一个条目也不好,因为我需要显示所有类型...

所以你能帮我做什么吗?我有点坚持下去。 (希望我的描述能够被理解)

This is my first post on this forum (so please be gentle )

I'm working on a huge database of products and I have got many tables of infos about them.

There is one table that looks like this:

product

  • product_id
  • name
  • brand

And another table that has the different types of the same product which looks like this:

product_type

  • type_id
  • type
  • costs
  • product_id

I use the join to select both tables and it works fine, but since there are more than a thousand entries in both tables I want to limit the selections and show only 15 entries on a page.

So here comes my problem:

I select the two tables joined and limit them 15. But since there are more types than products I select 15 product types which belong to an unknown number of products (since one product can have 1, 2 or any number of types). I need to be able to search in both tables and order the results according to columns from both and it's not good either to just select 15 entries from products and one entry from product_types for each, because I need all types to display...

So can you help me what should I do? I'm kinda stuck with it.
(I hope my description is understandable)

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

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

发布评论

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

评论(1

北方的韩爷 2025-01-13 03:15:22

有多种方法可以实现这一目标。我认为主要的困难是您需要对两个表中的列进行排序。因此,考虑到与类型的关系,前 15 个不同的产品将出现在可变数量的行中。最简单的方法可能是使用 GROUP_CONCAT< /a>

SELECT
    p.*,GROUP_CONCAT(t.type)
FROM
    products p INNER JOIN types t ON p.product_id=t.product_id
WHERE
    [your WHERE clauses]
GROUP BY
    p.product_id
ORDER BY
    [columns]
LIMIT 15

然而,这为您提供了有关您的类型的有限数据,并且可能会给您的 ORDER BY 带来问题,因为这种情况发生在 GROUP 之后。

您可能想做这样的事情

SELECT
    *
FROM
    ( SELECT
        DISTINCT p.*
    FROM
        products p INNER JOIN types t ON p.product_id=t.product_id
    WHERE
        [your WHERE clauses]
    ORDER BY
        [columns]
    LIMIT 15 ) pl
INNER JOIN types tl ON pl.product_id=tl.product_id

这应该做的是在连接到类型表并按您的条件排序时从产品表中获取前 15 个不同的行,然后获取这些产品的所有类型。

There are a number of ways to achieve this. The main difficulty as I see it is that you need to order on columns from both tables. Thus the first 15 distinct products will appear in a variable number of rows given the relationship with the types. The easiest way might be with a GROUP_CONCAT

SELECT
    p.*,GROUP_CONCAT(t.type)
FROM
    products p INNER JOIN types t ON p.product_id=t.product_id
WHERE
    [your WHERE clauses]
GROUP BY
    p.product_id
ORDER BY
    [columns]
LIMIT 15

However this gives you limited data about your types and may give you problems in your ORDER BY as this happens after the GROUP.

You probably want to do something like this

SELECT
    *
FROM
    ( SELECT
        DISTINCT p.*
    FROM
        products p INNER JOIN types t ON p.product_id=t.product_id
    WHERE
        [your WHERE clauses]
    ORDER BY
        [columns]
    LIMIT 15 ) pl
INNER JOIN types tl ON pl.product_id=tl.product_id

What this should do is get the first 15 DISTINCT rows from your products table when joined on your types table and ORDERed by your criteria, and then get all types for those products.

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