sql根據欄位的值判斷顯示其他欄位的值

時間 2021-09-06 05:50:06

1樓:韻藍玉夢

select

case when charindex(a,ans) >0 then a

else '' end as a,

case

when charindex(b,ans) >0 then belse '' end as b,

case

when charindex(c,ans) >0 then celse '' end as c,

case

when charindex(d,ans) >0 then delse '' end as d,

ansfrom baby

你這個設計的不太好,應該用兩個表,乙個表只存有幾個選項,另乙個表存答案

2樓:斯內科

使用分隔字串:

create   function   f_splitstrx(@sourcesql   varchar(8000),@strseprate   varchar(100))

returns   @temp   table(f1   varchar(100))

asbegin

declare   @ch   as   varchar(100)

set   @sourcesql=@sourcesql+@strseprate

while(@sourcesql<>'')

begin

set   @ch=left(@sourcesql,charindex(@strseprate,@sourcesql,1)-1)

insert   @temp   values(@ch)

set   @sourcesql=stuff(@sourcesql,1,charindex(@strseprate,@sourcesql,1),'')

endreturn

end--使用時

declare @ans varchar(200)

select @ans=ans from 表 where 題目id=5

declare @columns varchar(1000)

set @columns =''

select @columns=@columns+','+f1 from dbo.f_splitstrx('、',@ans)

set @columns=stuff(@columns,1,1,'')

exec ('select ' +@

+ ' from 表 where 題目id=5')

3樓:匿名使用者

什麼資料庫?下次提問一定要寫清楚是什麼環境哦,萬一別人費了半天勁結果還是資料庫不一樣多不好

sql根據乙個字段不同值查詢不同表中的乙個字段

4樓:匿名使用者

select case (role)

when 0 then (select name from tablea where dd=11)

else (select name from tableb where dd=11) end as name

from post

就是使用 case 我沒有實驗過。 如果有心在網上查一下 case的用法。

備註一下。 這是在 t_sql 裡面用的。

5樓:悠然解語

如果你要一次性全查出來只能用 union all:

select post.*,a.* from post join a on 連線條件 where post.role=1

union all

select post.*,a.* from post join b on 連線條件 where post.role=2

6樓:匿名使用者

樓主可以用

int role="select role from post......"

protected string getquanxian(int role)

return name;

} 希望能幫到樓主!

7樓:匿名使用者

post 和 a、b總有關聯的方法(條件)吧:

select a.name from a, post where

union all

select b.name from b, post where and post.role=2

8樓:晨_曦

提高乙個思路,先查詢出role的值,在用if語句判斷role的值是多少,相應的選擇表名進行第二個查詢。具體的功能就自己實現了哈。

如何在sql語句select中根據某個字段值,修改另外乙個欄位的值

9樓:語澍璐遠

update table set col2=case when col1 條件bai1 then 值

du1 when col1 條件2 then 值2;

或者分zhi為幾句修改dao

update table set col2=值1 where col1 條件1

update table set col2=值2 where col1 條件2

sql語句中怎麼判斷時間欄位為空值的

操作方法如下 如果想替換成別的,那麼。sql code?select from biao where isnull 你的字段,你替換成的值 sql語句中,能夠判斷 訂購日期 字段是否為空值的 1 列出 訂購日期 為空的記錄集。select from orders where 訂購日期 is null...

sql的欄位處理表中欄位為num現在想將其中的

select convert decimal 10,1 round 132451.27456,1 你看看這個式子的效果,結果132451.3正是你所想要的 update table set num convert decimal 10,1 round num,1 有困難hi我 血之暗夜精靈 orac...

sql查詢一條記錄多個字段對應另表字段的查詢語句如何寫

給個例子,可能還可以優化,其實這種複雜邏輯最好不要用sql去做,而是sql只是取出資料,邏輯在 層做。select t.航班號,t1.中文名,t1.英文名,t2.中文名,t2.英文名,t3.中文名,t3.英文名 from 表1 t,select 表1.航班號,表2.中文名,表2.英文名 from 表...