如何从 SQL 中的相关表中获取单个结果?

发布于 2024-12-02 14:19:47 字数 462 浏览 1 评论 0原文

我有以下 SQL 查询:

SELECT 
 gallery.id, 
 gallery.thumbnail_big, 
 products.id, 
 products.title, 
 products.size, 
 products.price, 
 products.text_description, 
 products.main_description 
FROM 
 gallery, 
 products 
WHERE gallery.id=products.id

它从 2 个表获取数据 - “产品”和“画廊”。表“图库”包含产品的图像。每个图像都有其 ID,该 ID 等于产品的 ID。每个产品都有多个图像,因此如果我按照上面指定的查询获取数据,我将获得每个产品的所有图像。不过我需要获取 1 张图像。每个产品 1 张图片。我知道我可以使用 DISTINCT 关键字来做到这一点。但是我不知道如何将其与我的查询一起使用。

I have the following SQL query:

SELECT 
 gallery.id, 
 gallery.thumbnail_big, 
 products.id, 
 products.title, 
 products.size, 
 products.price, 
 products.text_description, 
 products.main_description 
FROM 
 gallery, 
 products 
WHERE gallery.id=products.id

It gets data from 2 tables - "products" and "gallery". The table "gallery" contains images of the products. Each image has its ID which is equal to the ID of the product. Each product has several images so if I get the data as with the query specified above, I'll get all the images of each product. I need to get 1 image though. 1 image per a product. I know that I can do it using the DISTINCT keyword. However I can't figure out how that should be used with the query I have.

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

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

发布评论

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

评论(4

残花月 2024-12-09 14:19:47

由于您使用的是 MySQL,所以我将为您提供一个非常简单的 MySQL 特定解决方案:

SELECT 
 gallery.id, 
 gallery.thumbnail_big, 
 products.id, 
 products.title, 
 products.size, 
 products.price, 
 products.text_description, 
 products.main_description 
FROM gallery
INNER JOIN products 
ON gallery.id=products.id
GROUP BY products.id

当然,这会返回任意的 gallery.id 和thumbnail_big,但您还没有指定您想要哪一个。实际上,它将是第一个物理存储在表中的数据,但您对此几乎无法控制。

上面的查询是不明确的,因此 ANSI SQL 和大多数品牌的 RDBMS 都不允许它。但 MySQL 允许这样做(SQLite 也允许这样做,无论它的价值如何)。

更好的解决方案是使查询不产生二义性。例如,如果您想获取具有最高主键值的图库图像:

SELECT 
 g1.id, 
 g1.thumbnail_big, 
 p.id, 
 p.title, 
 p.size, 
 p.price, 
 p.text_description, 
 p.main_description 
FROM products p
INNER JOIN gallery g1 ON p.id = g1.id
LEFT OUTER JOIN gallery g2 ON p.id = g2.id AND g1.pkey < g2.pkey
WHERE g2.id IS NULL

我必须假设您有另一列自动递增的gallery.pkey,或者以其他方式用于唯一区分图库给定产品的图像。如果您没有这样的列,则需要创建一个。

然后,查询尝试查找同一产品的行 g2,该行大于 g1。如果不存在这样的行,则 g1 必定是最大的行。

Since you're using MySQL, I'll give you a MySQL-specific solution that's really easy:

SELECT 
 gallery.id, 
 gallery.thumbnail_big, 
 products.id, 
 products.title, 
 products.size, 
 products.price, 
 products.text_description, 
 products.main_description 
FROM gallery
INNER JOIN products 
ON gallery.id=products.id
GROUP BY products.id

Of course this returns an arbitrary gallery.id and thumbnail_big, but you haven't specified which one you want. In practice, it'll be the one that's stored first physically in the table, but you have little control over this.

The query above is ambiguous, so it wouldn't be allowed by ANSI SQL and most brands of RDBMS. But MySQL allows it (SQLite does too, for what it's worth).

The better solution is to make the query not ambiguous. For instance, if you want to fetch the gallery image that has the highest primary key value:

SELECT 
 g1.id, 
 g1.thumbnail_big, 
 p.id, 
 p.title, 
 p.size, 
 p.price, 
 p.text_description, 
 p.main_description 
FROM products p
INNER JOIN gallery g1 ON p.id = g1.id
LEFT OUTER JOIN gallery g2 ON p.id = g2.id AND g1.pkey < g2.pkey
WHERE g2.id IS NULL

I have to assume you have another column gallery.pkey that is auto-increment, or otherwise serves to uniquely distinguish gallery images for a given product. If you don't have such a column, you need to create one.

Then the query tries to find a row g2 for the same product, that is greater than g1. If no such row exists, then g1 must be the greatest row.

玩套路吗 2024-12-09 14:19:47

您需要指定您想要图库中的哪个值。您要求数据库执行的操作是任意删除您专门告诉它选择的记录。

有些人会做的是将 MAXMIN 添加到他们不想重复的列中。每次我看到这样做都是因为他们的查询中有错误的逻辑......

实际上试图从图库中选择什么?如果我们知道,我们可能可以帮助编写您的查询。

UPDATE

此方法使用 apply 仅获取每个产品的第一个图库记录(按 gallery.id 排序)。

SELECT 
 gallery.id, 
 gallery.thumbnail_big, 
 products.id, 
 products.title, 
 products.size, 
 products.price, 
 products.text_description, 
 products.main_description 
FROM  
 products p
OUTER APPLY
        (
        SELECT  TOP 1 g.id, g.thumbnail_big,
        FROM    gallery g
        WHERE   g.id= p.id
        ORDER BY
                g.id DESC
        ) g

You need to specify which of the values from gallery you want. What you are asking your database to do is to arbitrarily drop records that you specifically told it to select.

What some people will do is add a MAX or MIN to the columns they don't want to duplicate. Everytime I have seen this done it is because they have faulty logic in their query though....

What are you actually trying to select from gallery? If we know that we can probably help write your query.

UPDATE

This method uses apply to only get the first gallery record (ordered by gallery.id) for every product.

SELECT 
 gallery.id, 
 gallery.thumbnail_big, 
 products.id, 
 products.title, 
 products.size, 
 products.price, 
 products.text_description, 
 products.main_description 
FROM  
 products p
OUTER APPLY
        (
        SELECT  TOP 1 g.id, g.thumbnail_big,
        FROM    gallery g
        WHERE   g.id= p.id
        ORDER BY
                g.id DESC
        ) g
时光清浅 2024-12-09 14:19:47

通过/拥有组合的团体的完美候选人。

SELECT 
 gallery.id, 
 gallery.thumbnail_big, 
 products.id, 
 products.title, 
 products.size, 
 products.price, 
 products.text_description, 
 products.main_description 
FROM 
 products,
 gallery 

WHERE gallery.id=products.id
GROUP BY products.id
HAVING MIN(gallery.id)

将返回您所有产品,每个产品一张图片。然而,图像的选择是具有最小 id 的图像。

Perfect candidate for the group by/having combination.

SELECT 
 gallery.id, 
 gallery.thumbnail_big, 
 products.id, 
 products.title, 
 products.size, 
 products.price, 
 products.text_description, 
 products.main_description 
FROM 
 products,
 gallery 

WHERE gallery.id=products.id
GROUP BY products.id
HAVING MIN(gallery.id)

Will return you all products, one image each. However, the choice for the image was the image having the minimum id.

离笑几人歌 2024-12-09 14:19:47

您并没有说显示哪张图片很重要,因此这是获取一张图片的最简单方法:

SELECT 
 products.id, 
 products.title, 
 products.size, 
 products.price, 
 products.text_description, 
 products.main_description,
 MAX(gallery.thumbnail_big) AS thumbnail_big, 
FROM 
 products,
 gallery
WHERE
 products.id = gallery.id
GROUP BY
 products.id, 
 products.title, 
 products.size, 
 products.price, 
 products.text_description, 
 products.main_description,

You don't say that it matters which pic you display, so this is the easiest way to get just one:

SELECT 
 products.id, 
 products.title, 
 products.size, 
 products.price, 
 products.text_description, 
 products.main_description,
 MAX(gallery.thumbnail_big) AS thumbnail_big, 
FROM 
 products,
 gallery
WHERE
 products.id = gallery.id
GROUP BY
 products.id, 
 products.title, 
 products.size, 
 products.price, 
 products.text_description, 
 products.main_description,
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文