從多個表中查詢資料的sql語句,sql一個表中同時查詢兩個count的sql語句

2022-02-22 00:54:08 字數 6063 閱讀 3359

1樓:

建立一個儲存過程用來讀取

create procedure testpercudureasdeclare @col integerdeclare @tablename varchar(100)declare @sql as varchar(200)begin

select top 0 qqnum into dbo.qqnum from groupdata.dbo.group001 --建立一個表用來裝你的資料

set @col=1

set @tablename='st'

while @db_num<=100

begin

set @sql='select * from '+@tablename+@col+' group'+' where sname = '李三'' --通過迴圈獲取你的表名和資料庫名,之後組裝成sql語句,然後執行.你試試. 我沒資料庫不好修改的

execute sp_executesql @sqlset @col=@col+1

set @col=1

endend

大概像上面的儲存過程一樣,這樣要求表名是st1,st2,... ,st100

2樓:

chen_hongyang的回答是合理的,其實這個問題還得看你的具體需求,還有資料庫的設計是不是這樣的,是資料庫中表結構就這樣設計的

3樓:情又獨中

union all

比如select * from st01 where sname='李三'

union all

select * from st02 where sname='李三'

union all

....................

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

sql一個表中同時查詢兩個count的sql語句是什麼?

mysql中查詢兩個表同一條件的資料條數該怎麼寫sql語句

4樓:匿名使用者

不知道自

你的baia,b兩表有沒有

du關zhi聯,假dao定沒有關聯

select count(1)

from

(select id

from a

where id>5

union all

select id

from b

where id>5)

5樓:匿名使用者

select * from a where id>5

union all

select * from b where id>5

6樓:曉瑩兒

select count(1)

from

(select id

from a

where id>5

union all

select id

from b

where id>5

)as `表名`

sql語句 怎麼把從一個表中查出來資料插入到另一個表中

7樓:鬱筱羽

標準sql語句

bai格式:

insert

into 表名(

du欄位zhi

名)select 欄位名

from 表面

例子:dao將內查詢出的s表中容sno,j表中jno,p表中pno插入spj表中

insert

into spj(sno,jno,pno)select sno,jno,pno

from s,j,p

8樓:sql的藝術

insert into table2 (col1,col2,col3)

select col1,col2,col3 from table1

記得插入表的列數要與查詢結果列數一致,並且資料格式也需一致

9樓:育知同創教育

使用insert into 目標表(欄位列表) select 欄位列表 from 原始表

即可實現你所說的功能。

10樓:匿名使用者

你要查什麼資料據?算了,我這是巢狀語句,你看著往裡面換欄位就可以了

insert into 表(select 條件 from 表)

11樓:

很簡單 就是一bai個du

inert into table(col1,col2,…)select col1,col2,… 語句例如:insert into a(id,name) select id,name from emp;

表示zhi從emp表中查dao

詢出來的

id,name值專 插入到屬a表的id,name中

12樓:尹巧駿

(1).select * into desttbl from srctbl

(2).insert into desttbl(fld1, fld2) select fld1, 5 from srctbl

以上兩句都是將 srctbl 的資料插入到 desttbl,但兩句又有區別的:

第一句(select into from)要求目內標表(desttbl)不存在,因容為在插入時會自動建立。

第二句(insert into select from)要求目標表(desttbl)存在,由於目標表已經存在,所以我們除了插入源表(srctbl)的欄位外,還可以插入常量,如例中的:5。

13樓:匿名使用者

insert into table_dest(column1, column2...)select column1, column2...

from table_source

where ....

14樓:匿名使用者

insert into t1 value (select * from t2);

15樓:楊春三月

insert  into  users1(id,name,age)  select  users.user_id,users.user_name,users.

user_age   from  users ;

親測試可用!

內!容!

sql中,如何查詢存在一個表而不在另一個表中的資料記錄 20

16樓:匿名使用者

首先,在sql中(以sql server為例),查詢存在一個表而不在另一個表中的資料記錄的方法有很多,介紹其中4種:

1、方法一(僅適用單個欄位):使用 not in ,比較容易理解,缺點是效率低

如:select a.id from a where a.id not in (select id from b);

2、方法二(適用多個欄位匹配):使用 left join...on... , "b.id isnull" 表示左連線之後在b.id 欄位為 null的記錄。

如:select a.id from a left join b on a.id=b.id where b.id is null ;

3、方法三(適用多個欄位匹配)

如:select * from b where (select count(1) as num from a where a.id = b.id) = 0;

4、方法四(適用多個欄位匹配)

如:select * from a where not exists(select 1 from b where a.id=b.id)

接著,我們來分析你的sql語句為什麼返回資料不準確的原因。

從你的sql基礎語句來看,你使用了方法一和方法四這兩種,兩種語法本身都是正確的,但是卻沒有達到預期的效果,初步分析,問題可能出在gsdj和swdj這兩張表的qymc欄位的判斷比較上。

舉個例子:'企業名稱'和'企業名稱  '這兩個字串看似相同,實際卻並不相同,因為第二個「企業名稱 」的後面跟了一個空格字元。就因為這個空格字元導致這個"'企業名稱'='企業名稱 '"等式不成立。

考慮到你qymc這個欄位的型別是字元型,建議你在原有sql基礎上做一個微調如下:

select * from gsdj  gs where not exists (select * from swdj sw where rtrim(ltrim(sw.qymc )) )=rtrim(ltrim(gs.qymc )));

其中ltrim()可以去除左側空格,rtrim()可以去除右側的空格,也就是說我們是對去除空格後的企業名稱進行比較,排除了空格的干擾。

擴充套件資料:

在sql中,對於字元型文字資料,經常需要用到去空格的操作,對oracle資料來說可以通過trim()函式來簡單實現,而sql server中並沒有trim()函式,只有ltrim()和rtrim()兩個函式。

sql 中使用ltrim()去除左邊空格 ,rtrim()去除右邊空格 ,沒有同時去除左右空格的函式,要去除所有空格可以用replace(字串,' ',''),將字串裡的空格替換為空。

例:去除空格函式

declare @temp char(50)

set @temp = ' hello sql '

print ltrim(@temp)     --去除左邊空格

print rtrim(@temp)     --去除右邊空格

print replace(@temp,' ','') --去除字串裡所有空格

print @temp

>> 輸出結果

hello sql

hello sql

hellosql

hello sql

17樓:妗妗歘歘

我有兩張表如何查詢在一個表姑在另一個表中的資料

18樓:煙染暖陽

select * from swdj where qymc not in (select qymc from gsdj)

19樓:匿名使用者

select * from gsdj t1 where not exists (select * from swdj where qymc=t1.qymc )

20樓:匿名使用者

select * from gsdj gsdj where gsdj.qymc not in (select swdj.qymc from swdj swdj) 或者

select * from gsdj gs where not exists (select * from swdj sw where sw.qymc=gs.qymc )

試試加上表別名

21樓:丶我是週週

select * from gsdj where gsdj.qymc =swdj.qymc and gsdj.

qymc not in (select swdj.qymc from swdj )這兩個表之間必須要有一個相連線的列

22樓:匿名使用者

select * from gsdj where not exists (select * from swdj where gsdj.qymc=swdj.qymc)

23樓:鎖映僪鶴騫

只需判斷一下即可,根據你的題目意思應該是a表的id和b表的id相關聯。

select *, case when (select count(*) from b where id = a.id)>0 then 1 else 0 end as flag from a如果你是想a表和b表的欄位和id這兩列都一樣,才將flag顯示為1的話,用下面的查詢:

select *, case when (select count(*) from b where id = a.id and 欄位 = a.欄位)>0 then 1 else 0 end as flag from a

用sql語句查詢某表中擁有多個相同欄位中的值

希望以下 回答能幫回助您答 select from case where class select top 1 class from case group by class order by count class desc 用sql語句查詢某表中擁有多個相同欄位中的一個值 欄位1欄位2,欄位3,欄...

sql語句怎麼從一張表中查詢資料插入到另一張表中

查詢的資料bai插入到另一張表中du,分為zhi兩種情況 一dao種是目標表不存在,專另一種是目屬標表存在。工具 oracle 10g 源表資料 情況一 目標表不存在,建立表名為t1的表,將person表中全部資料插入 執行語句 create table t1 as select from pers...

求sql查詢語句,查詢資料庫中三張表

select a.wtlx from 問題列表 a,受理表 b where a.id b.id and convert varchar 10 b.chtime,120 between 2014 07 01 and 2014 08 01 union all select b.wjlx from 問題列...