SQL中如何對比兩表之間的差異

時間 2022-07-29 22:45:18

1樓:匿名使用者

建立表create table table1

(id int,

uid varchar(10))

insert into table1 values (1,12)

insert into table1 values (2,1234)

insert into table1 values (3,1245)

insert into table1 values (4,1356)

insert into table1 values (5,13)

insert into table1 values (6,133478)

create table table2

(id int,

uid varchar(10))

insert into table2 values (1,12)

insert into table2 values (2,1234)

insert into table2 values (3,1245)

insert into table2 values (4,1356)

insert into table2 values (5,13)

insert into table2 values (6,133478)

insert into table2 values (7,12345)

insert into table2 values (8,13455)

insert into table2 values (9,13558)

執行select left(t.uid,2) 開頭數字,count(*) 數量

from

(select * from table2 except select * from table1) t

where left(t.uid,2) in ('12','13')

group by left(t.uid,2)結果

2樓:匿名使用者

用except可以解決,比如:

select nid from table2except

select nid from table1然後在外層做一次group by即可

select substring(1,2,nid) 開頭數字,count(1) 數量 from (

select nid from table2 exceptselect nid from table1 ) as t1

group  by substring(1,2,nid)

3樓:

select nid,count(nid) from

(select left(nid,2) nid from table2 where nid not in (select uid from table1)) as a group by nid

4樓:匿名使用者

select substr(t2.nid,0,3),count(*) from table2 t2 where t2.nid not in (select t1.

nid from table1 t1) group by (substr(t2.nid,0,3))

你試一下 沒有自測 有問題在問

sql表中同時查詢兩個count的sql語句

可以有兩種解決方法。方法1 select name count 1 as 總題數 sum case when statu 1 then 1 else 0 end as 稽核題數 from question group by nme 方法2 select s.總題數,s.稽核題數,s.name fro...

關於資料庫表與表之間的聯絡,SQL資料庫中表與表之間的關聯怎麼建立

資料庫由若干個表構成。表與表之間通過主鍵和外來鍵構成關係。主鍵和外來鍵是相對的。這個表的主鍵,可能就是另一個表的外來鍵。或者說,一個鍵,在這個表中是主鍵,而在另一個表中可能就是外來鍵了。主鍵與外來鍵只是區別於他的功能。 雁子 一對一 例如t person表和t card表,即人和身份證。這種情況需要...

兩張EXCEL表中的資料做對比如何操作啊,表如下,要對比出單價變動的資料是哪幾條?注意單價裡面相同價格

單價整列複製到同一張表,單價減單價,再篩選不等於0的。在表二d2中 sum if 表一 a 2 a 5 a2,if 表一 c 2 c 5 c2,1,0 注意 ctrl shift enter 複製公式即可 結果 1 為無變動 0 為有變動 放在一個工作表中比對 遠光財務軟體 是國內第一套完全以瀏覽器...