如何从 SQL 中的相关表中获取单个结果?
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您使用的是 MySQL,所以我将为您提供一个非常简单的 MySQL 特定解决方案:
当然,这会返回任意的 gallery.id 和thumbnail_big,但您还没有指定您想要哪一个。实际上,它将是第一个物理存储在表中的数据,但您对此几乎无法控制。
上面的查询是不明确的,因此 ANSI SQL 和大多数品牌的 RDBMS 都不允许它。但 MySQL 允许这样做(SQLite 也允许这样做,无论它的价值如何)。
更好的解决方案是使查询不产生二义性。例如,如果您想获取具有最高主键值的图库图像:
我必须假设您有另一列自动递增的
gallery.pkey
,或者以其他方式用于唯一区分图库给定产品的图像。如果您没有这样的列,则需要创建一个。然后,查询尝试查找同一产品的行
g2
,该行大于g1
。如果不存在这样的行,则g1
必定是最大的行。Since you're using MySQL, I'll give you a MySQL-specific solution that's really easy:
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:
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 thang1
. If no such row exists, theng1
must be the greatest row.您需要指定您想要图库中的哪个值。您要求数据库执行的操作是任意删除您专门告诉它选择的记录。
有些人会做的是将
MAX
或MIN
添加到他们不想重复的列中。每次我看到这样做都是因为他们的查询中有错误的逻辑......您实际上试图从图库中选择什么?如果我们知道,我们可能可以帮助编写您的查询。
UPDATE
此方法使用 apply 仅获取每个产品的第一个图库记录(按
gallery.id
排序)。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
orMIN
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.通过/拥有组合的团体的完美候选人。
将返回您所有产品,每个产品一张图片。然而,图像的选择是具有最小 id 的图像。
Perfect candidate for the group by/having combination.
Will return you all products, one image each. However, the choice for the image was the image having the minimum id.
您并没有说显示哪张图片很重要,因此这是获取一张图片的最简单方法:
You don't say that it matters which pic you display, so this is the easiest way to get just one: