我在同一张表中有以下3个字段:
cell | home | primary
----------------------------------------------
111 2222 cell
456565 4654564 home
我希望根据cell
字段的值达到home
或primary
的值。
我尝试了以下操作,这显然不起作用......但无法弄清楚如何将primary
的值转换为列名:
SELECT
(SELECT primary FROM tblstudents WHERE studentid = 39358)
FROM
tblstudents WHERE studentid = 39358
由于
答案 0 :(得分:3)
您可以使用CASE
:
SELECT
CASE primary WHEN 'cell' THEN cell ELSE home END AS ContactNo,
primary
FROM TableName
答案 1 :(得分:1)
答案 2 :(得分:0)
select primary ,
case
when primary = 'cell' then cell
when primary = 'home' then home
END as phone
from tblstudents
WHERE studentid = 39358
答案 3 :(得分:0)
以防您不喜欢CASE选择器:
SELECT cell phone FROM students WHERE PRIMARY='cell'
UNION
SELECT home phone FROM students WHERE PRIMARY='home'