我有一张桌子,上面有其他2个数据库(文本文件和htmltables)中的数据
他们的共同点是order_number列,这就是为什么我将它们组合到同一张表中的原因。 dr_ *来自文本文件,oi_ *来自htmltable

Select * from data;
+--------------+-----------+----------+-----------+-------+
| order_number | dr_amount | dr_speed | oi_amount | oi_up |
+--------------+-----------+----------+-----------+-------+
|         9699 | 10000     | 26000    | NULL      | NULL  |
|         9699 | 20000     | 47619    | NULL      | NULL  |
|        10135 | 18000     | 12676    | NULL      | NULL  |
|         9979 | 25000     | 14286    | NULL      | NULL  |
|         9699 | NULL      | NULL     | 4800      | 4     |
|        10135 | NULL      | NULL     | 8700      | 2     |
|         9979 | NULL      | NULL     | 3000      | 8     |
+--------------+-----------+----------+-----------+-------+


首先,我必须使用以下命令从表中挑选出唯一的order_number(带有dr_amount)

select order_number, count(*) as c from data where oi_amount IS NOT NULL group by order_number having c<2;
+--------------+---+
| order_number | c |
+--------------+---+
|         9699 | 1 |
|         9979 | 1 |
+--------------+---+


这将删除具有9699的order_number,因为它位于2行中&& dr_amount = IS NOT NULL
(根据以后的处理,如果特定行是重复行,则无法将特定行与另一行匹配,因此,我排除了所有dr_amount = IS NOT NULL的重复项)

接下来,我想通过将没有重复的order_number(与相同order_number的dr_amount相同)与具有oi_ *值的order_number相结合来产生这种输出。像这样:

+--------------+-----------+----------+-----------+-------+
| order_number | dr_amount | dr_speed | oi_amount | oi_up |
+--------------+-----------+----------+-----------+-------+
|        10135 | 18000     | 12676    | 8700      | 2     |
|         9979 | 25000     | 14286    | 3000      | 8     |
+--------------+-----------+----------+-----------+-------+


如您所见,由于不重复的排序,order_number 9699被排序,并且第3行和第6行以及第4行和第7行被合并。

我在考虑使用第一个过滤器获取非重复的order_number并将其传递给第二个选择查询,作为where =内的结果,但这给我带来了问题:

select order_number, dr_amount, dr_speed, oi_amount, oi_up from data where order_number=(select order_number, count(*) as c from data where oi_amount IS NOT NULL group by order_number having c<2);

Operand should contain 1 column(s)


我了解为什么会发生错误,但无法解决。使用排序重复项所需的嵌套选择时,它将返回2列(order_number和count(*)为c)。
那么如何使用嵌套选择,以便在将其传递给真实选择时仅包含1列?

最好的祝福
尼古拉斯·古斯塔夫森(Niclas Gustafsson)

最佳答案

在该上下文中的子查询不能返回两列。您还需要in而不是=,因为它可能返回多个行。这可能是您要尝试执行的操作:

select order_number, dr_amount, dr_speed, oi_amount, oi_up
from data
where order_number in (select order_number
                       from data
                       where oi_amount IS NOT NULL
                       group by order_number
                       having count(*) < 2
                      ) and
      dr_amount is not null;

关于mysql - mySQL-根据第一行的计数合并行的结果?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25219251/

10-13 06:53