[英]Compare two or more columns in sql
數據庫結構:
question
qid, que, a, b, c, d, ans
虛擬數據1:
Qid-1
Que-what is capital of india ?
A- mumbai
B- delhi
C-kolkata
D-chennai
Ans-B
虛擬數據2:
Qid-2
Que-formula of water ?
A- h2O
B- h30
C-h40
D-h50
Ans-A
所需輸出:
Que ans
what is capital of india ? Delhi
Formula of water ? H20
如何比較最后一列( ans
)與其他列( a, b, c, d
)?
我需要一個SQL查詢。
提前致謝。
當我理解時,您需要將ans
解碼為適當的列。 假設答案'A'的意思是:a列的返回值,答案'B'-b列的返回值,等等,在Oracle中,我會這樣做(未經實際表測試):
SELECT que, decode(ans, 'A', a, 'B', b, 'C', c, 'D', d, 'No answer') FROM question
帕維爾
這樣嘗試
select que,
switch(
ans = 'A', A
, ans = 'B', B
, ans = 'C', C
, ans = 'D', D
)
from table1
要么
select que, IIf(ans='A', A, IIf(ans='B',B,IIf(ans='C',C,D))) from table1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.