我有桌子:
ID, firstname, lastname, companyName, isCompany
如何运行查询,以便我可以执行:
SELECT ID, name FROM myTable ...
其中名称是
行的算法if(isCompany)
name = companyName
else
name = "lastname, firstname"
答案 0 :(得分:2)
您没有说明您的DBMS,但这是ANSI SQL:
select id,
case
when is_company then companyName
else lastname||', '||firstname
end as name
from my_table
这假定is_company
的类型为boolean