在mysql中如何选择不同的cols并选择其他列?

时间:2021-12-07 23:19:43

I'd like to select any distinct combos of columns a+b AND select column c

我想选择a+b列的任意组合,然后选择c列

The sql is basically this:

sql基本上是这样的:

SELECT DISTINCT (a, b), c
FROM mytable

Error returned: Operand should contain 1 column(s)

返回的错误:操作数应该包含1列

is this even possible?

这是可能吗?

1 个解决方案

#1


3  

You want to use group by instead:

你想用group by代替:

SELECT a, b, c
FROM mytable
group by a, b;

Distinct works on all the columns, not just a few. This formulation returns an arbitrary value of c from one of the rows. More typically, you would choose a value, such as:

不同的作品在所有的列上,而不仅仅是少数。这个公式从其中一行返回任意值c。更典型的是,您将选择一个值,例如:

SELECT a, b, min(c)
FROM mytable
group by a, b;

#1


3  

You want to use group by instead:

你想用group by代替:

SELECT a, b, c
FROM mytable
group by a, b;

Distinct works on all the columns, not just a few. This formulation returns an arbitrary value of c from one of the rows. More typically, you would choose a value, such as:

不同的作品在所有的列上,而不仅仅是少数。这个公式从其中一行返回任意值c。更典型的是,您将选择一个值,例如:

SELECT a, b, min(c)
FROM mytable
group by a, b;