简体   繁体   English

比较SQL中的两个或更多列

[英]Compare two or more columns in sql

Database structure : 数据库结构:

  • Table name : question 表名: question
  • Columns : qid, que, a, b, c, d, ans 列: qid, que, a, b, c, d, ans

Dummy data 1: 虚拟数据1:

Qid-1
Que-what is capital of india ?
A- mumbai
B- delhi
C-kolkata
D-chennai
Ans-B

Dummy data 2: 虚拟数据2:

Qid-2
Que-formula of water ?
A- h2O
B- h30
C-h40
D-h50
Ans-A

Desired output : 所需输出:

Que                                ans
what is capital of india ?         Delhi
Formula of water ?                 H20

How to compare last column ( ans ) with other columns ( a, b, c, d )? 如何比较最后一列( ans )与其他列( a, b, c, d )?

I need a SQL query for that.. 我需要一个SQL查询。

Thanks in advance. 提前致谢。

As i inderstood you need decode ans into proper column. 当我理解时,您需要将ans解码为适当的列。 Assuming that answer 'A' means: return value of column a, answer 'B' - return value of column b etc, in Oracle I'd do it like this (not tested on real table): 假设答案'A'的意思是:a列的返回值,答案'B'-b列的返回值,等等,在Oracle中,我会这样做(未经实际表测试):

SELECT que, decode(ans, 'A', a, 'B', b, 'C', c, 'D', d, 'No answer') FROM question

Pawel 帕维尔

Try like this 这样尝试

select que,
  switch(
    ans = 'A', A
  , ans = 'B', B
  , ans = 'C', C
  , ans = 'D', D
  )
from table1

OR 要么

   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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM