sql資料對比和篩選,sql 在同一張表篩選出一對多或一對一的資料?

時間 2022-12-03 07:05:32

1樓:

if not exists(select * from a1 inner join a2 on a1.a=a2.a)

--再往a3裡面插入資料

insert into a3

select 'a,'+a1.a+','+a2.a from a1 left outer join a2 on 1=1

--其他列重複

2樓:

insert into c(t)

select 'b'

from a1, a2

where a1.a = a2.a

and a1.b <> a2.b

union all

select a1.b

from a1, a2

where a1.a = a2.a

and a1.b <> a2.b

union all

select a2.b

from a1, a2

where a1.a = a2.a

and a1.b <> a2.b

union all

select 'c'

from a1, a2

where a1.a = a2.a

and a1.c <> a2.c

union all

select a1.c

from a1, a2

where a1.a = a2.a

and a1.c <> a2.c

union all

select a2.c

from a1, a2

where a1.a = a2.a

and a1.c <> a2.c

union all

select 'd'

from a1, a2

where a1.a = a2.a

and a1.d <> a2.d

union all

select a1.d

from a1, a2

where a1.a = a2.a

and a1.d <> a2.d

union all

select a2.d

from a1, a2

where a1.a = a2.a

and a1.d <> a2.d

union all

select 'e'

from a1, a2

where a1.a = a2.a

and a1.e <> a2.e

union all

select a1.e

from a1, a2

where a1.a = a2.a

and a1.e <> a2.e

union all

select a2.e

from a1, a2

where a1.a = a2.a

and a1.e <> a2.e

union all

select 'f'

from a1, a2

where a1.a = a2.a

and a1.f <> a2.f

union all

select a1.f

from a1, a2

where a1.a = a2.a

and a1.f <> a2.f

union all

select a2.f

from a1, a2

where a1.a = a2.a

and a1.f <> a2.f

sql 在同一張表篩選出一對多或一對一的資料?

3樓:匿名使用者

你可以採用分組的方式來獲取

用sql語句實現資料篩選

4樓:琳

--將字段條件篩選建立臨時表

select top 100 *

into #temp

from table

where name not in ("%批發%","不含'%廠")

and region in ("餐飲",..."副食")--填寫完每個經營面

--返回資料表,企業數和個體戶,這個區分不知道用什麼,所以第二個欄位還需要改

select province,count(distinct name) as num_company,

case when name="個體戶" then count(distinct name) as num_individual

from #temp

5樓:雲南新華電腦學校

您說的這個意思我的理解是使用臨時表。

例如1.插入臨時表

select a,b,c into #tmpfrom table1

2.使用臨時表

select * from #tmp

這時候臨時表內的資料就是您篩選出來的資料,可視為正常資料表來使用。

sql server 2014中怎麼篩選兩列不同的值

6樓:匿名使用者

繳費資訊比水表資訊多的值:

select distinct 繳費資訊 from 表名except

select distinct 水表資訊 from 表名水表資訊比繳費資訊多的值:

select distinct 水表資訊 from 表名except

select distinct 繳費資訊 from 表名你說的是這個意思?

7樓:匿名使用者

用多的左關聯少的,那樣就可以知道少了哪些,兩列的話就拆分成兩次select * from

(select 繳費資訊 from tablename) aleft join

(select 水表資訊 from tablename) bon a.繳費資訊=b.水表資訊

sql資料庫中 如何將兩個表中共同的資料進行比對篩選

8樓:匿名使用者

select fnsfhm from sfgaylfn where fnsfhm not in (select fnsfhm from jbqk )

多選擇篩選查詢sql語句怎麼寫 100

9樓:匿名使用者

看你的選擇感覺上應該是「選擇的全部」,比如,我選了wifi,餐桌,麻將機,那麼就必須這三個都有,其他的有或沒有不重要,重要的是這三個,也就是說「選擇的全部」。

方法想到了三個:

(1)用like+and,無非是 字段 like '%15%' and 字段 like '%17%' and ……,有多少個選擇寫多少個like,然後根據傳進來的引數擷取(這部分最好是應該是前台完成,當然寫成過程也可以),然後再匯入資料庫查詢。(這個重複查詢多,估計效率不會太快)

(2) 直接like也可以,只是這種需要臨時拼湊語句,前面的select不說,where後面是:字段 like '%15%21%31%……',把數字中間填上%,這部分要麼改變語句傳輸的間隔符號,要麼做一下替換. (個人比較推薦這種方式,但是這種方式要注意排序,順序是不能亂的,否則查不出來)

(3)你說的in應該不行,至少我不會,但是find_in_set可以考慮,和like+and差不多,只是又多了一層判斷,而且會出現大量的"不等於0",個人覺得並不會十分好用。

10樓:卡洛琳

乙個欄位的篩選條件用模糊查詢就行了,你字段裡的數字都是排序的,可以用like

比如facilities like '%條件值1%條件值2%條件值3%'

11樓:匿名使用者

用行列轉換吧把fac這字段拆分為多行資料,或者用儲存過程執行動態sql比如

begin

declare @t nvarchar(100),@sql nvarchar(100);

set @t= (select '1,2,3,11,12,13' as c)

set @sql='select * from (

select ''1,2,3,11,12,13'' a,''1'' b

union all

select ''21,22,23,4,5,6'' a,''2'' b ) t1'

execute(@sql+' where ''1'' in ('+@t+')')

end這個只是例子,根據實際情況改用,在sql sever中有個xml可以進行行列轉換拆分的。mysql沒有研究過

12樓:匿名使用者

資料庫操作用什麼?如果是用mybatis的話可以寫迴圈乙個乙個遍歷,如下:

select * from t_employee where id in

#如果是其他的就在實現層進行遍歷。

13樓:匿名使用者

我做過類似的查詢,就是用字典項表的資料id列,與資料表的字串列做instr比較,比較時,兩個資料分別在前後加',',防止第乙個和最後乙個字典項無法查詢出來。

舉個例子,資料表

字典表 dict

id name

12 wifi

13 冰箱

14 洗衣機

15 電視

業務表 query_table

id query_str

1 12,13,14,15

2 12,14

3 14,15

select *

from dict a,query_table bwhere instr(concat(concat(',',b.query_str),','), concat(',', concat(a.id,',')))>-0 and b.

id=1

看實際效果

ASP問題?SQL語法問題,SQL資料庫和ACC資料庫之間的不同之處

sqlnotice select from abl notice order if systemdatabasetype sql then sqlnotice sqlnotice by elite desc noticeid desc elseif systemdatabasetype access...

在oracle資料庫中怎麼通過sql語句查詢表的Primary Key

select from dba constraints dc where dc.constraint type p and dc.table name 然後可以用plsql develooper 工具檢視建表sql,就知道對那個字段建立主鍵 select column name from dba c...

在p系統下怎樣安裝sql資料庫,在xp系統下怎樣安裝SQL資料庫?

把光碟放進光區 點選安裝 資料庫伺服器 下一步 本地計算機 建立新的sql severe實力或客戶端工具 下一步 是 伺服器和客戶端工具 預設 下一步 典型 選擇你的安裝路徑然後點選 下一步 使用本地系統帳戶 下一步 windows身份驗證 下一步 下一步 安裝 完成 安裝步驟就是這樣簡單,如果是安...

在SQL中,建立 修改和刪除資料庫中基本表結構的命令分別為

方法一 新增 adodc.recordset.activeconnection.execute insert into table1.我個人覺得用adodc最簡單 二 dim adorst asnew adodb.recordset adorst.addnew adorst.fields admin...

在本機上測試asp程式,但是sql資料庫,我應該怎麼辦

要安裝ms sql server 2000 將ms sql server 2000企業版光碟放入光碟機,出現安裝引導介面 選擇 安裝 sql server 2000元件 選擇 安裝資料庫伺服器 開始安裝過程。出現以下介面時,選擇第二項 伺服器和客戶端工具,按 下一步 注意下面的常見錯誤!如果您嘗試在...