各個大神,專案期間遇到一個問題,需求如下
表中有兩列,其中第一列的中資料在第二列存在,第二列的資料在第一列中存在,相互存在的需要合并成同一條記錄,請教如何實作,
表資料如下
A B
1 2
2 1
1 3
3 4
4 3
現在要查詢出來的結果
A B
1 2
1 3
3 4
uj5u.com熱心網友回復:
SQL>
SQL> create table test(a int, b int);
Table created
SQL> begin
2 insert into test values(1, 2);
3 insert into test values(2, 1);
4 insert into test values(1, 3);
5 insert into test values(3, 4);
6 insert into test values(4, 3);
7 end;
8 /
PL/SQL procedure successfully completed
SQL> select distinct least(a, b) a, greatest(a,b) b
2 from test;
A B
---------- ----------
1 2
1 3
3 4
SQL> drop table test purge;
Table dropped
SQL>
uj5u.com熱心網友回復:
方法不錯
假如加入
insert into test values(8, 6);
這條記錄,是不是展示時有點問題
uj5u.com熱心網友回復:
嗯,a,b 的顯示前后順序會變。 不知道會不會對樓主的實際需求有沒有影響;
uj5u.com熱心網友回復:
select t1.*
from test t1,test t2
where t1.a=t2.b and t1.b=t2.a and t1.a<t1.b
union
select * from test t where (t.a,t.b) not in
(select t1.*
from test t1,test t2
where t1.a=t2.b and t1.b=t2.a);
SQL>
SQL> create table test(a int, b int);
Table created
SQL> begin
2 insert into test values(1, 2);
3 insert into test values(2, 1);
4 insert into test values(1, 3);
5 insert into test values(3, 4);
6 insert into test values(4, 3);
7 end;
8 /
PL/SQL procedure successfully completed
SQL> select distinct least(a, b) a, greatest(a,b) b
2 from test;
A B
---------- ----------
1 2
1 3
3 4
SQL> drop table test purge;
Table dropped
SQL>
方法不錯
假如加入
insert into test values(8, 6);
這條記錄,是不是展示時有點問題
delete from tt
where a in (
select t2.a
from tt t1,tt t2
where t1.a = t2.b and t2.a = t1.b and t2.a > t2.b
)
這樣就沒問題了
uj5u.com熱心網友回復:
select * from tb a,tb b
where a.列1=b.列2 and a.列2=b.列1
如果你要顯示的是必須相同的話。。。
uj5u.com熱心網友回復:
select a,b from table
union
select b,a from table
uj5u.com熱心網友回復:
select t1.*
from test t1,test t2
where t1.a=t2.b and t1.b=t2.a and t1.a<t1.b
union
select * from test t where (t.a,t.b) not in
(select t1.*
from test t1,test t2
where t1.a=t2.b and t1.b=t2.a);
**桔妹導讀:**深耕人工智能領域,致力于探索AI讓出行更美好的滴滴AI Labs再次斬獲國際大獎,這次獲獎的專案是什么呢?一起來看看詳細報道吧! 近日,由國際計算語言學協會ACL(The Association for Computational Linguistics)舉辦的世界最具影響力的機器 ......
我們經常在資料庫中使用 LIKE 運算子來完成對資料的模糊搜索,LIKE 運算子用于在 WHERE 子句中搜索列中的指定模式。 如果需要查找客戶表中所有姓氏是“張”的資料,可以使用下面的 SQL 陳述句: SELECT * FROM Customer WHERE Name LIKE '張%' 如果需要 ......
關于MySQL的二進制日志(binlog),我們都知道二進制日志(binlog)非常重要,尤其當你需要point to point災難恢復的時侯,所以我們要對其進行備份。關于二進制日志(binlog)的備份,可以基于flush logs方式先切換binlog,然后拷貝&壓縮到到遠程服務器或本地服務器 ......