简体   繁体   English

如何添加两个不同行的两列在MySQL中具有相同的字段?

[英]how to add two columns of two different rows have one same field in MySQL?

I have a MySQL database: results: 我有一个MySQL数据库:结果:

ID           |    B_ID     |  SUM
------------ |-------------|---------
 1           |    400      |   10
 2           |    500      |   20
 3           |    500      |   30
 4           |    400      |   40

But i want this: 但是我想要这个:

ID           |   B_ID      |  SUM
-------------|-------------|---------
 1           |    400      |   50
 2           |    500      |   50

Assuming that results is an actual table, you can query it as follows: 假设results是一个实际表,则可以按以下方式查询它:

SELECT MIN(ID),
       B_ID,
       SUM(SUM)
FROM results
GROUP BY B_ID

If by "results" you mean that results is the output from another query, then, without knowing what your original table looks like, you could subquery as follows: 如果用“结果”表示results是另一个查询的输出,那么在不知道原始表是什么样的情况下,可以按如下所示进行子查询:

SELECT MIN(t.ID),
       t.B_ID,
       SUM(t.SUM)
FROM
(
    -- your original query goes here
) t
GROUP BY t.B_ID

SQL Fiddle SQL小提琴

暂无
暂无

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

相关问题 如何在GridView中有两列具有相同字段但值不同 - How to have two columns with the same field but different values in a GridView 如何从MySQL表中提取两个具有相同字段值而另一字段不同的行? - How do I pull two rows from a MySQL table that have one field value in common and the other field different? 如何在MySQL的同一张表上的两个不同输入值之间的两个不同列之间选择行 - How to select rows between two different columns with two different input values on same table on mysql MYSQL - 如何在同一个表上的两个不同行上减去两列? - MYSQL - How may i subtract two columns on two different rows, on the same table? 如何使用mysql在datagridview的两个不同列中显示具有相同ID的两行? - How to show two rows with same id in two different columns in datagridview using mysql? 将两个查询合并到同一个表中的一个(相同列/不同行) - Combine two queries into one in same table (same columns/different rows) Mysql选择行两列不具有相同值 - Mysql Select Rows Where two columns do not have the same value MySQL - 将两列拆分为两个不同的行 - MySQL - Split two columns into two different rows 如何选择或区分同一表中的两行,其中一个字段的值相同,而另一字段不同 - how to select or distignuish two rows in the same table with the same value on one field but different on another PHP和MYSQL - 如何检查两行或更多行是否具有相同的字段值 - PHP & MYSQL - How To Check If Two Or More Rows Have The Same Field Value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM