我需要帮助来编写SQL和上下文是,
-sample数据如下:
a,b, count
1,2, 10
4,5, 20
2,1, 5
5,4, 6
6,7, 10 --逻辑:
A和b的行与其相反的行,即b和a(其中a=b和b=a)匹配。例如,对于1,2行,2,1行被颠倒,并且它们之间预期的总计数为15
预期结果集:
1,2 15
4,5,26
6,7,10或2,1,15 4,5,26 6,7,10
任何简单的SQL都能得到预期的结果,这会很有帮助。
发布于 2014-11-18 15:17:21
使用自连接:
mysql> create table test (a integer, b integer, count integer);
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test values(1,2,10);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test values(4,5,20);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test values(2,1,5);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test values(5,4,6);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT distinct a.a, a.b,a.count + b.count from test a
join test b on a.a = b.b where a.a< a.b;
+------+------+-------------------+
| a | b | a.count + b.count |
+------+------+-------------------+
| 1 | 2 | 15 |
| 4 | 5 | 26 |
+------+------+-------------------+
2 rows in set (0.00 sec)
mysql>https://stackoverflow.com/questions/26988542
复制相似问题