简体   繁体   English

相当于MS-Access函数first()last()的MYSQL

[英]MYSQL equivalent of MS-Access function first() last()

To better explain what I need please look at this table: 为了更好地解释我需要什么,请查看下表:

ID --- image
A1 --- a1_01.jpg
A1 --- a1_02.jpg
B7 --- b7_01.jpg
B7 --- b7_02.jpg
D3 --- D3_04.jpg
D3 --- D3_99.jpg
... (From A to Z)
Z9 --- Z9_12.jpg
Z9 --- Z9_13.jpg
Z9 --- Z9_20.jpg
Z9 --- Z9_99.jpg
...and so on

I need the results to be: 我需要的结果是:

ID --- Image
A1 --- a1_01.jpg
B7 --- b7_01.jpg
D3 --- D3_04.jpg
...
Z9 --- Z9_12.jpg
...and so on

In MS-Access this is can be done with SELECT ID, First(image) AS 'Image' FROM Photos 在MS-Access中,可以使用SELECT ID, First(image) AS 'Image' FROM Photos来完成

How can I accomplish this in MYSQL? 如何在MYSQL中完成此操作?

Remember that this is a mockup table, to illustrate my problem. 请记住,这是一个样机表,用来说明我的问题。 I have tons and tons of records. 我有无数的记录。 IT IS NOT a single (least/most) value! 它不是一个(最小/最大)值!

===EDIT: 11/24/2012=== ===编辑:11/24/2012 ===
FINAL SQL CODE: 最终的SQL代码:

SELECT ID, Min(image) AS 'Image' 
FROM Photos 
GROUP BY ID

Thank you @triclosan & @iDevlop !!! 谢谢@triclosan@iDevlop !!!

As i may see 如我所见

select ID, min(image)
from tbl
group by ID
SELECT ID, Min(image) AS 'Image' FROM mockup GROUP BY ID

If the first image is also the image with the minimum value, and the last image is also the image with the maximum value, this MySql query returns exactly what you want: 如果第一个图像也是具有最小值的图像,而最后一个图像也是具有最大值的图像,则此MySql查询将完全返回您想要的内容:

Select ID, min(image) as first_image, max(image) as last_image
From Tbl
Group By ID

otherwise, since MySql has no first() or last() aggregate function, you could use something like this: 否则,由于MySql没有first()last()聚合函数,因此可以使用如下代码:

Select
  Distinct ID,
  (select image from tbl a where a.ID = tbl.ID order by something ASC LIMIT 1) as first_image,
  (select image from tbl a where a.ID = tbl.ID order by something DESC LIMIT 1) as last_image,
From tbl

but notice that you have to order your table by some field. 但请注意,您必须按某个字段对表格进行排序。 If you can't order your table by some field, there's no way to tell which value comes first and which one last. 如果无法按某个字段对表进行排序,则无法分辨哪个值排在前,哪个值在后。

I often see this query, that looks nice and that is supposed to return the first field: 我经常看到这个查询,看起来不错,应该返回第一个字段:

Select ID, image
From tbl
Group by ID

this a non-standard Sql query that usually works and (almost?) always returns correctly the first image. 这是一个通常可以正常工作的非标准Sql查询,并且(几乎?)总是正确返回第一个图像。 But since image is not in the group by clause and is not associated to any aggregate function, the server is free to return any value. 但是,由于image不在group by子句中,并且未与任何聚合函数相关联,因此服务器可以自由返回任何值。 It seems that it always returns the first value, but it's not guaranteed and I prefer not to use it. 似乎它总是返回第一个值,但不能保证,我更喜欢不使用它。

Some explanations: 一些解释:

In your example, the minimum always coindices with the first, so there's no way to tell from your example if you are looking for the minimum or if you are looking for the first. 在您的示例中,最小值始终与第一个一致,因此无法从示例中看出是要查找最小值还是要查找第一个。

Suppose this is your data: 假设这是您的数据:

ID  image
=============
A1  a1_02.jpg
A1  a1_01.jpg

If you are looking for the minimum ( A1, a1_01.jpg ), min(image) is exactly what you need and it will work! 如果您正在寻找最小值( A1, a1_01.jpg ),那么min(image)正是您所需要的,它将起作用!

But if you are trying to get A1, a1_02.jpg , which in this example is the first... then we have a problem. 但是,如果您尝试获取A1, a1_02.jpg ,则在此示例中是第一个...那么我们就遇到了问题。 Without a specific ORDER BY, the rows will be returned in an unpredictable way. 没有特定的ORDER BY,行将以不可预测的方式返回。 Yes, they will often/almost always be returned in the order they are physically in the tablespace, but we can't rely on this. 是的,它们经常/几乎总是按照它们在表空间中的顺序返回,但是我们不能依靠它。

See for example this question and the answers: 例如,请参阅此问题和答案:

SQL best practice to deal with default sort order SQL最佳实践,处理默认排序顺序

This means that if we don't know how the rows are ordered, we can't tell which one is the first, and which the last. 这意味着,如果我们不知道行的排序方式,就无法确定哪个是第一行,最后一个是哪行。 See for example this query: 例如,请参见以下查询:

Select
  Distinct ID,
 (select image from tbl a where a.ID = tbl.ID LIMIT 1) as first_image
From tbl

This will probably return what you are looking for. 这可能会返回您要查找的内容。 But since I omitted the ORDER BY clause, there's no guarantee that it will always work. 但是由于我省略了ORDER BY子句,所以不能保证它会一直工作。 And notice that i also had to remove last_image, because there's no way to tell MySql to order the table by default order DESC, since such default order doesn't exist. 并请注意,我还必须删除last_image,因为没有办法告诉MySql按默认顺序DESC对表进行排序,因为这种默认顺序不存在。

This query will probably also return the first image: 该查询可能还会返回第一个图像:

Select ID, image
From tbl
Group By ID

In standard SQL we cannot refer to nonaggregated columns in the select, but in MySql it is possible. 在标准SQL中,我们不能在select中引用未聚合的列,但在MySql中是可以的。 And it will probably return the first image for every ID. 并且它可能会为每个ID返回第一个图像。 But let's see the documentation: 但是让我们看一下文档:

http://dev.mysql.com/doc/refman/5.0/en/group-by-extensions.html http://dev.mysql.com/doc/refman/5.0/en/group-by-extensions.html

"The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate." “服务器可以从每个组中自由选择任何值,因此,除非它们相同,否则选择的值是不确定的。” So the fact that the server almost always returns the first row is just coincidence, and it's not documented. 因此,服务器几乎总是返回第一行的事实只是一个巧合,而没有记录在案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM