Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.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
使用If-else条件选择mysql中的列?_Mysql - Fatal编程技术网

使用If-else条件选择mysql中的列?

使用If-else条件选择mysql中的列?,mysql,Mysql,我有两张这样的桌子。两张桌子是分开的 AccountNo User Name ---------------------------------- 1 U a 2 U b 3 U c 另一个表包含以下结构 TempAccountNo Mycolumn AccountNo ------

我有两张这样的桌子。两张桌子是分开的

AccountNo       User         Name
----------------------------------
 1               U            a
 2               U            b
 3               U            c
另一个表包含以下结构

 TempAccountNo       Mycolumn    AccountNo     
------------------------------------------
 4               X                2341
 5               Y                 2
 6               Z                2568
我需要从表II中选择AccountNo或TempAccountNo、Mycolumn,条件为

If (tableII.AccountNo not in table I)
我需要从表II中选择
TempAccountNo

else
我需要从表II中选择
AccountNo

else

如何实现这一点。

我使用if。。。else语句或using case语句。下文对此进行了描述

   SELECT IF(t1.AccountNo IS NULL, t2.TempAccountNo, t2.AccountNo) AS optional_field
     FROM table2 t2
LEFT JOIN t1 ON t1.AccountNo = t2.AccountNo
  • 使用if语句选择MYSQL

    select IF('fieldname with condition','if true value','if false value') from table_name where 1;
    
  • MYSQL使用多个if-else选择

    select 
      CASE
        WHEN fieldname1 = 'value1' THEN 'if true then give value'
        WHEN fieldname2 = 'value2' THEN 'if true then give value' THEN 'if true then give value'
        WHEN fieldname3 = 'value3' THEN 'if true then give value' THEN 'if true then give value' 
        WHEN fieldname4 = 'value4' THEN 'if true then give value' THEN 'if true then give value'
        ELSE 'else all false then give default value'
      END as STATUS,
    from table_name where 1;
    
  • 尝试使用这些查询

    SELECT IF(t1.AccountNo IS NULL, t2.TempAccountNo, t2.AccountNo) AS Acc_common
    FROM table1 t1
    INNER JOIN table2 t2 ON t1.AccountNo = t2.AccountNo
    

    这将有助于……

    为什么这是最被接受的答案?它有什么作用?