简体   繁体   English

如何 select 基于列值的值?

[英]How to select a value based on a column value?

Suppose, Member table has a column named Contact假设, Member表有一个名为Contact的列

If Contact has value, Then Type = 1 Otherwise, Type will be blank如果 Contact 有值,则 Type = 1 否则,Type 将为空

What will be the SQL to retrieve Contact and Type检索联系人类型的 SQL 将是什么

You can use case statement :您可以使用case statement

SELECT contact, CASE WHEN contact IS NOT NULL THEN 1 AS type
FROM membertable

You can use IIF() function which is introduced and supported after SQL Server 2012.您可以使用在 SQL Server 2012 之后引入和支持的IIF() function。

Syntax:句法:

IIF (boolean_expression, true_value, false_value) IIF (boolean_expression, true_value, false_value)

Use:利用:

SELECT concat,  IIF(COALESCE(contact, '') != '', '1', '') AS [type]
FROM Member

Just use a case in your query to generate the type column.只需在查询中使用一个案例来生成类型列。

SELECT 
    contact,
    CASE
       WHEN contact IS NOT NULL then 1
       ELSE ''
    END as type
FROM Member

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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