sql語句判斷一張表的某欄位為空,然後查詢另外一張表的資料,怎麼寫

2021-07-08 21:09:28 字數 5570 閱讀 6441

1樓:匿名使用者

樓上那個不行,應該是a沒有再找b 而不是連線2個表一起找樓主你的**的問題出在when id is null 你都輸入id=10289了

id怎麼會空?

應該是select case

when a.name is null thenb.name

else

a.name

end name

from (select name from a where id = '10289') a,(select name from b where id = '10289') b

2樓:匿名使用者

select t.id,t.name from( select id,name from a union select id,name from b) t

group by t.id,t.name

這樣查詢每個記錄只會存在一條,這樣查詢出來的是最全的,不過不知道那個是a 表那個是b表的,它查詢出來的是a.b的彙總,但是是唯一的

3樓:匿名使用者

試試我的儲存過程如何?

create or replace procedure a_sel_b

asname varchar2(20);

tmp varchar2(10);

begin

select count(1) into tmp from a where a.id='10289';

if tmp=0 then

select b.name into name from b where b.id='10289';

else

select a.name into name from a where a.id='10289';

end if;

dbms_output.put_line(name);

commit;

end;如果我的**執行無報錯,那麼

begin

a_sel_b;

end;

試試吧,有問題可以繼續問我~

4樓:匿名使用者

你的case when 後面的欄位錯了 應該是name 下面的才是正確的

select

case when name is null then (select name from b where id='10289')

else (select name from a where id='10289') end

from a

where id='10289'

5樓:匿名使用者

select top 1 name from(select name, 1 as xh from a where id=10289

union all

select name, 2 as xh from b where id=10289

) order by xh

6樓:匿名使用者

select

case

when a.id is not null then a.nameelse b.name end

from

a full join b on (a.id = b.id )

where

a.id='10289' or b.id='10289'

sql server中如何操作"如果一個欄位為空,則將另一個表中的某欄位賦值給他"

7樓:匿名使用者

update a set a.a = b.bfrom b

where a.id = b.id

and a.a is null;

sql 資料庫,一個表中的資料 在另一個表中查詢,如果沒有則顯示結果

8樓:匿名使用者

select * from 表1 where 欄位抄 not in (select 欄位襲 from 表bai2)若是還要考慮du到表二zhi有,而表一沒有的dao的情況select * from 表1,表2 where 欄位 not in (select 欄位 from 表1 inner join 表2 on 表1.欄位=表2.欄位)

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

9樓:匿名使用者

首先,在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

10樓:妗妗歘歘

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

11樓:煙染暖陽

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

12樓:匿名使用者

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

13樓:匿名使用者

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 )

試試加上表別名

14樓:丶我是週週

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

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

15樓:匿名使用者

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

16樓:鎖映僪鶴騫

只需判斷一下即可,根據你的題目意思應該是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查詢一個表中兩個欄位對應的另一個表的資料,應該怎麼操作?

17樓:匿名使用者

根據 news表中的 news_type_id = 1 查出 news_type表中的 「透明點評」 這條資料,「透明點評」是最後需要查出來的位置資料。

子查詢或者表連線

比如表連線的方式就可以寫成:

select n.id,t.type_name,title from news as n inner join news_type as t onnn.

news_type_id=t.type_id;

只查「透明點評」的資料子查詢可以寫成:

select * from news where news_type_id=(select type_id from news_type where type_name='透明點評');

18樓:匿名使用者

sql查詢一個表中兩個欄位對應的另一個表的資料,可以通過如下操作進行:輸入語句:select a.

* from test a,test1 b where a.id=b.id and a.

name=b.name;

sql怎麼將一張表的欄位賦值給另一張表

更新還bai是插入數du據?插入資料zhi 1insert into tbytz userid select userid from tbuser 更新資料則dao在tbuser和tbytz兩個表要回有一個關係。答。如tbuser.a1 tbytz.a2 1update tbytz set tbyt...

SQL語句判斷某一表中要新增的欄位變數是否在該表的欄位變數中存在

各個資料庫判斷的標準不一樣。如果是ms sql server 你可以使用if not exists select 1 from sysobjects a join syscolumns b on a.id b.idwhere a.id object id table name and b.name ...

sql語句怎麼查詢表的欄位屬性名

請問你bai使用的是什麼資料庫?如果是dusql server你可以使用如zhi下的語句dao來獲取各個表的欄位名稱及其專相關的屬性.select from syscolumns where id select id from sysobjects where name 你要查詢的屬表名 看了看但不...