Sql 使用多个列和限制2分组?

Sql 使用多个列和限制2分组?,sql,mariadb,mariadb-10.4,Sql,Mariadb,Mariadb 10.4,我的mysql数据库中有以下数据结构: +-------+----------+-------+------------+-------------+-------------+ | Color | brand | size | otherstuff | otherstuff2 | otherstuff3 | +-------+----------+-------+------------+-------------+-------------+ | red | BMW |

我的mysql数据库中有以下数据结构:

+-------+----------+-------+------------+-------------+-------------+
| Color |  brand   | size  | otherstuff | otherstuff2 | otherstuff3 |
+-------+----------+-------+------------+-------------+-------------+
| red   | BMW      | small | 0,69       | 0,22        | 0,36        |
| red   | BMW      | big   | 0,48       | 0,00        | 0,13        |
| red   | Ford     | small | 0,42       | 0,33        | 0,81        |
| red   | BMW      | big   | 0,99       | 0,66        | 0,27        |
| red   | Ford     | big   | 0,30       | 0,44        | 0,23        |
| red   | Mercedes | small | 0,87       | 0,23        | 0,80        |
| red   | Mercedes | big   | 0,51       | 0,06        | 0,46        |
| blue  | BMW      | small | 0,07       | 0,53        | 0,99        |
| blue  | BMW      | small | 0,57       | 0,45        | 0,45        |
| blue  | BMW      | big   | 0,14       | 0,23        | 0,37        |
| blue  | Ford     | big   | 0,83       | 0,14        | 0,20        |
| blue  | Ford     | big   | 0,20       | 0,10        | 0,60        |
| blue  | Mercedes | small | 0,58       | 0,90        | 0,90        |
| blue  | BMW      | small | 0,12       | 0,41        | 0,40        |
| blue  | Mercedes | big   | 0,81       | 0,57        | 0,79        |
| blue  | Mercedes | big   | 0,95       | 0,36        | 0,70        |
| green | Ford     | big   | 0,09       | 0,56        | 0,90        |
| green | Ford     | small | 0,54       | 0,03        | 0,99        |
| green | Ford     | small | 0,86       | 0,58        | 0,12        |
| green | BMW      | small | 0,88       | 0,39        | 0,91        |
| green | BMW      | small | 0,39       | 0,92        | 0,50        |
| green | BMW      | big   | 0,11       | 0,62        | 0,23        |
+-------+----------+-------+------------+-------------+-------------+
我希望收到每种颜色和每种品牌的一排大号和一排小号-如下所示:

+-------+----------+-------+------------+-------------+-------------+
| Color |  brand   | size  | otherstuff | otherstuff2 | otherstuff3 |
+-------+----------+-------+------------+-------------+-------------+
| red   | BMW      | small | 0,69       | 0,22        | 0,36        |
| red   | BMW      | big   | 0,48       | 0,00        | 0,13        |
| red   | Ford     | small | 0,42       | 0,33        | 0,81        |
| red   | Ford     | big   | 0,30       | 0,44        | 0,23        |
| red   | Mercedes | small | 0,87       | 0,23        | 0,80        |
| red   | Mercedes | big   | 0,51       | 0,06        | 0,46        |
| blue  | BMW      | small | 0,57       | 0,45        | 0,45        |
| blue  | BMW      | big   | 0,14       | 0,23        | 0,37        |
| blue  | Ford     | big   | 0,20       | 0,10        | 0,60        |
| blue  | Mercedes | big   | 0,81       | 0,57        | 0,79        |
| blue  | Mercedes | small | 0,58       | 0,90        | 0,90        |
| green | Ford     | big   | 0,09       | 0,56        | 0,90        |
| green | Ford     | small | 0,54       | 0,03        | 0,99        |
| green | BMW      | small | 0,39       | 0,92        | 0,50        |
| green | BMW      | big   | 0,11       | 0,62        | 0,23        |
+-------+----------+-------+------------+-------------+-------------+
我的SQL:

SELECT * FROM `table` GROUP BY color

我的SQL是什么样子的,每种颜色有一个小品牌和一个大品牌?

谢谢@Nick和所有其他人。 这对我很有用:

SELECT color, brand, size, otherstuff1, otherstuff2, otherstuff3 FROM [table] GROUP BY color, brand, size ORDER BY color, brand, size

感谢您

为每种颜色、品牌和尺寸选择一行,任意使用行号:


您是否关心otherstuff值,或者它们可以是该颜色和大小中的任何值?哪个MySQL版本?@Nick otherstuff可以是任何值。只需按颜色、品牌、大小分组。您通常按所选的相同列分组,但设置函数的参数除外。不过,这是无效的SQL。MySQL和MariaDB可能会忽略这一点,但它们会在非分组列(即otherstuff列)上默默地应用任何_值。请注意,通过此查询,结果行可以包含从不同行中拾取的otherstuff1、otherstuff2、otherstuff3,尽管这种情况不太可能发生。由于您似乎缺乏使用GROUPBY的经验,我建议您仔细阅读并设置sql_mode='ONLY_FULL_GROUP_BY';指导您,直到您完全确定自己在做什么,例如,了解功能依赖的含义。阅读此博客可能有助于您了解群组成员:。它是关于MySQL在几年前的一个变化,通过检测聚合查询中的函数依赖性来实现标准遵从性。就我所知,MariaDB仍然缺乏这种提升。我认为这篇文章很好地解释了GROUP BY的作用,以及在使用此子句时可以选择哪些列。@ThorstenKettner-Thans获取提示和链接。@Ben。这是断开的SQL-SELECT和GROUP BY列不匹配。它只是不能保证做你想做的事。Thorsten的回答是正确的,尽管这不是唯一可能的正确解决方案。谢谢大家。稍后我将尝试@ThorstenKettner的代码。这非常有效。非常感谢你。如果我想要大小=大的行中的maxotherstuff和大小=小的行中的minotherstuff,我必须更改什么@thorstenThis将不再随意选取行,而是寻找特定的聚合值。听起来像是带有条件聚合的分组查询。我建议您查找条件聚合并尝试它。如果你自己不能解决这个问题,请在新的单独请求中提出。
SELECT color, brand, size, otherstuff, otherstuff2, otherstuff3
FROM
(
  SELECT
    t.*,
    ROW_NUMBER() OVER (PARTITION BY color, brand, size ORDER BY otherstuff) AS rn
  FROM mytable t
) numbered
WHERE rn = 1
ORDER BY color, brand, size;