Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何在sql中使用条件运算符_Mysql_Sql_Sql Server 2008_Sql Server 2008 R2 - Fatal编程技术网

Mysql 如何在sql中使用条件运算符

Mysql 如何在sql中使用条件运算符,mysql,sql,sql-server-2008,sql-server-2008-r2,Mysql,Sql,Sql Server 2008,Sql Server 2008 R2,我想找回的是联系电话。我有一个名为BusinessDetails的关系,字段是Name,Description,PhoneNum,MobileNum。联系电话的情况如下: 我可以同时拥有MobileNum和PhoneNum 我可以要任何一个 我不能两者兼得 我正在尝试这段SQLSELECT语句,当这两条语句都有时,效果非常好。但是在2和3的情况下,有不必要的逗号与之关联。我的sql是 SELECT Business.PhoneNum+','+Business.MobileNum AS Co

我想找回的是联系电话。我有一个名为BusinessDetails的关系,字段是
Name
Description
PhoneNum
MobileNum
。联系电话的情况如下:

  • 我可以同时拥有MobileNum和PhoneNum
  • 我可以要任何一个
  • 我不能两者兼得
  • 我正在尝试这段SQLSELECT语句,当这两条语句都有时,效果非常好。但是在
    2
    3
    的情况下,有不必要的逗号与之关联。我的sql是

       SELECT Business.PhoneNum+','+Business.MobileNum AS ContactNumber 
         FROM dbo.BusinessDetails AS Business
    
    有人能帮我去掉第二和第三种情况下的逗号吗?

    使用,如下所示:

    select CONCAT_WS(',', Business.PhoneNum, Business.MobileNum) 
    from dbo.BusinessDetails as Business
    

    您可以使用
    case
    (适用于几乎所有DBMS的通用解决方案):


    哪个产品,哪个版本?SQL Server或MySQL?@Arunesh标签在文章中提到的
    MySQL
    MySQL仅适用于SQL ans SQL Server r2。
    select case
             when PhoneNum is null then -- MobileNum only
               MobileNum
             when MobileNum is null then -- PhoneNum only
               PhoneNum
             when (PhoneNum is not null) and (MobileNum is not null) then -- Both
               MobileNum || ',' || PhoneNum
             else -- Neither MobileNum no PhoneNum  
               'No Phone'
           end as ContactNumber      
      from dbo.BusinessDetails as Business