MySQL:选择2列和3列的最大组合

MySQL:选择2列和3列的最大组合,mysql,Mysql,我有以下设备表: | id | name | width | height | storage_aspect_ratio | created_at | updated_at | status | type | |------|-------------------------------------------------------|----

我有以下设备表:

| id   | name                                                  | width   | height   | storage_aspect_ratio | created_at          | updated_at          | status   | type   |
|------|-------------------------------------------------------|---------|----------|----------------------|---------------------|---------------------|----------|--------|
| 18   | Apple Cinema Display 30                               | 2560    | 1600     | 8:5                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 1      |
| 19   | Apple iMac 21.5-inch (Late 2009 - Late 2012)          | 1921    | 1080     | 1921:1080            | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 1      |
| 20   | Apple iMac 21.5-inch (Retina 4K Display)              | 4096    | 2304     | 16:9                 | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 1      |
| 21   | Apple iMac 27-inch (Late 2009 - Late 2012)            | 2560    | 1440     | 16:9                 | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 1      |
| 22   | Apple iMac 27-inch (Retina 5K Display)                | 5120    | 2880     | 16:9                 | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 1      |
| 23   | Apple iPad 1 & 2                                      | 768     | 1024     | 3:4                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 2      |
| 24   | Apple iPad 3 & 4                                      | 1536    | 2048     | 3:4                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 2      |
| 25   | Apple iPad Air                                        | 2048    | 1536     | 4:3                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 2      |
| 26   | Apple iPad Air 2                                      | 2048    | 1536     | 4:3                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 2      |
| 27   | Apple iPad Mini                                       | 768     | 1024     | 3:4                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 2      |
| 28   | Apple iPad Mini 2                                     | 2048    | 1536     | 4:3                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 2      |
| 29   | Apple iPad Mini 4                                     | 2048    | 1536     | 4:3                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 2      |
| 30   | Apple iPad Pro                                        | 2732    | 2048     | 683:512              | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 2      |
| 31   | Apple iPad Pro 9.7                                    | 2048    | 1536     | 4:3                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 2      |
| 32   | Apple iPhone 11                                       | 828     | 1792     | 207:448              | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 3      |
| 33   | Apple iPhone 11 Pro                                   | 1125    | 2436     | 375:812              | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 3      |
| 34   | Apple iPhone 11 Pro Max                               | 1242    | 2688     | 207:448              | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 3      |
| 35   | Apple iPhone 3GS                                      | 320     | 480      | 2:3                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 3      |
| 36   | Apple iPhone 4 (4, 4S)                                | 640     | 960      | 2:3                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 3      |
| 37   | Apple iPhone 5 (5c, 5s)                               | 640     | 1136     | 40:71                | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 3      |
| 38   | Apple iPhone 6                                        | 750     | 1334     | 375:667              | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 3      |
| 39   | Apple iPhone 6 Plus                                   | 1080    | 1920     | 9:16                 | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 3      |
| 40   | Apple iPhone 6s                                       | 750     | 1334     | 375:667              | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 3      |
| 41   | Apple iPhone 7                                        | 750     | 1334     | 375:667              | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 3      |
| 42   | Apple iPhone 7 Plus                                   | 1080    | 1920     | 9:16                 | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 3      |
| 43   | Apple iPhone 8                                        | 750     | 1334     | 375:667              | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 3      |
| 44   | Apple iPhone 8 Plus                                   | 1080    | 1920     | 9:16                 | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 3      |
| 45   | Apple iPhone SE                                       | 640     | 1136     | 40:71                | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 3      |
| 46   | Apple iPhone X                                        | 1125    | 2436     | 375:812              | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 3      |
| 47   | Apple iPhone XR                                       | 828     | 1792     | 207:448              | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 3      |
| 48   | Apple iPhone XS                                       | 1125    | 2436     | 375:812              | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 3      |
| 49   | Apple iPhone XS Max                                   | 1242    | 2688     | 207:448              | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 3      |
| 50   | Apple MacBook 12-inch                                 | 2304    | 1440     | 8:5                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 1      |
| 51   | Apple MacBook Air 11-inch                             | 1366    | 768      | 683:384              | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 1      |
| 52   | Apple MacBook Air 11-inch (Late 2010 - Mid 2012)      | 1366    | 768      | 683:384              | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 1      |
| 53   | Apple MacBook Air 13-inch                             | 1440    | 900      | 8:5                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 1      |
| 54   | Apple MacBook Air 13-inch (Late 2010 - Mid 2012)      | 1440    | 900      | 8:5                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 1      |
| 55   | Apple MacBook Pro 13-inch                             | 2560    | 1600     | 8:5                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 1      |
| 56   | Apple MacBook Pro 13-inch (Mid 2009 - Mid 2012)       | 1280    | 800      | 8:5                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 1      |
| 57   | Apple MacBook Pro 13-inch (Retina display)            | 2560    | 1600     | 8:5                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 1      |
| 58   | Apple MacBook Pro 15-inch                             | 2880    | 1800     | 8:5                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 1      |
| 59   | Apple MacBook Pro 15-inch (Early 2006 - Mid 2012)     | 1440    | 900      | 8:5                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 1      |
| 60   | Apple MacBook Pro 15-inch (Hi-Res screen)             | 1680    | 1050     | 8:5                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 1      |
| 61   | Apple MacBook Pro 15-inch (Retina display)            | 2880    | 1800     | 8:5                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 1      |
| 62   | Apple MacBook Pro 17-inch (Mid/Late 2007 - Late 2011) | 1920    | 1200     | 8:5                  | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 1      |
| 63   | Apple Thunderbolt Display 27                          | 2560    | 1440     | 16:9                 | 2020-12-15 17:16:02 | 2020-12-24 16:24:28 | 1        | 1      |


我需要根据宽度和高度的最大组合来获得不同的存储\u纵横比。我能得到的最接近的查询是下面的查询,但我认为它仍然很遥远

SELECT width, height, storage_aspect_ratio
FROM wallpaper_device
    NATURAL JOIN
       (SELECT GREATEST(width, height)
        FROM wallpaper_device
        GROUP BY width, height) AS max
WHERE status = 1
GROUP BY width, height, storage_aspect_ratio
ORDER BY width DESC;
返回整行数据名称、宽度、高度、存储空间、纵横比、状态和类型也很好。这就是为什么我没有使用distinct,因为该名称将始终返回所有结果。这可能吗?或者我应该在代码中过滤它

仔细想想,这个名字并不重要,那么可以使用distinct吗

我想得到这个结果:

| width | height | storage_aspect_ratio | status | type |
|-------|--------|----------------------|--------|------|
| 2880  | 1800   | 8:5                  | 1      | 1    |
| 1921  | 1080   | 1921:1080            | 1      | 1    |
| 5120  | 2880   | 16:9                 | 1      | 1    |
| 1536  | 2048   | 3:4                  | 1      | 2    |
| 2048  | 1536   | 4:3                  | 1      | 2    |
| 2732  | 2048   | 683:512              | 1      | 2    |
| 1242  | 2688   | 207:448              | 1      | 3    |
| 1125  | 2436   | 375:812              | 1      | 3    |
| 640   | 960    | 2:3                  | 1      | 3    |
| 640   | 1136   | 40:71                | 1      | 3    |
| 750   | 1334   | 375:667              | 1      | 3    |
| 1080  | 1920   | 9:16                 | 1      | 3    |
| 1366  | 768    | 683:384              | 1      | 1    |
那么:

select storage_aspect_ratio, max(width * height) as pixels
from wallpaper_device
where status = 1
group by 1
order by 2 desc
请参阅。

如何:

select storage_aspect_ratio, max(width * height) as pixels
from wallpaper_device
where status = 1
group by 1
order by 2 desc

请参阅。

使用波希米亚人查询,但在order by中添加MAX。见下文

SELECT storage_aspect_ratio, MAX(width * height) AS pixels
FROM wallpaper_device
WHERE STATUS = 1
GROUP BY storage_aspect_ratio
ORDER BY MAX(width * height) DESC

这样你就不会收到令人恼火的错误消息。将所有列添加到组by将击败您试图获得的最大值。这就是为什么这样做会得到这么多结果。

使用Bohemians查询,但按顺序添加MAX。见下文

SELECT storage_aspect_ratio, MAX(width * height) AS pixels
FROM wallpaper_device
WHERE STATUS = 1
GROUP BY storage_aspect_ratio
ORDER BY MAX(width * height) DESC

这样你就不会收到令人恼火的错误消息。将所有列添加到组by将击败您试图获得的最大值。这就是为什么这样做会得到这么多结果。

回答问题,并将所需结果与给定的样本数据一起添加为表。@stickybit我以为我已经包含了结果集。我更新了我的问题,并意识到名称并不重要。宽度和高度的最大组合可以是宽度和高度的最大乘积吗?最大宽度*高度?用给定的样本数据作为表格,回答问题并添加所需的结果。@stickybit我以为我已经包含了结果集。我更新了我的问题,并意识到名称并不重要。宽度和高度的最大组合可以是宽度和高度的最大乘积吗?ie maxwidth*height?我尝试了类似的方法,但我的SQL编辑器给了我一个错误:ORDER BY子句的表达式1不在GROUP BY子句中,并且包含未聚合的列“wallpaper\u device.width”,它在功能上不依赖GROUP BY子句中的列;这与sql\u mode=only\u full\u组不兼容_by@Nfourteenoops-现在试试。我仍然会遇到这个错误,我唯一可以执行查询的方法是:从墙纸设备中选择存储\u纵横比,maxwidth*height作为像素,其中status=1 group by width,height,storage\u aspect\u ratio by width*height desc;但随后我得到了多个相同的存储\u纵横比结果。@n即使查询有效:请参阅实时演示。此外,按宽度、高度、存储空间和纵横比分组也无法正常工作。尝试将我的查询复制粘贴到您的环境中。若要查看错误消息,我使用了:从墙纸设备中选择存储\u纵横比,maxwidth*高度作为像素,其中状态=1按存储分组\u纵横比按像素顺序描述;谢谢@Bohemian的帮助!我尝试了类似的方法,但我的SQL编辑器给了我一个错误:ORDER BY子句的表达式1不在GROUP BY子句中,并且包含未聚合的列“wallpaper_device.width”,它在功能上不依赖于GROUP BY子句中的列;这与sql\u mode=only\u full\u组不兼容_by@Nfourteenoops-现在试试。我仍然会遇到这个错误,我唯一可以执行查询的方法是:从墙纸设备中选择存储\u纵横比,maxwidth*height作为像素,其中status=1 group by width,height,storage\u aspect\u ratio by width*height desc;但随后我得到了多个相同的存储\u纵横比结果。@n即使查询有效:请参阅实时演示。此外,按宽度、高度、存储空间和纵横比分组也无法正常工作。尝试将我的查询复制粘贴到您的环境中。若要查看错误消息,我使用了:从墙纸设备中选择存储\u纵横比,maxwidth*高度作为像素,其中状态=1按存储分组\u纵横比按像素顺序描述;谢谢@Bohemian的帮助!