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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4688782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:19:27  来源:igfitidea点击:

use the If else condition for selecting the column in mysql?

mysql

提问by svk

I am having two tables like this.Both are separate tables

我有两张这样的桌子。都是单独的桌子

AccountNo       User         Name
----------------------------------
 1               U            a
 2               U            b
 3               U            c

And another table contains the following structure

另一个表包含以下结构

 TempAccountNo       Mycolumn    AccountNo     
------------------------------------------
 4               X                2341
 5               Y                 2
 6               Z                2568

I need to select the AccountNo or TempAccountNo,Mycolumn From table II and the condition is

我需要从表 II 中选择 AccountNo 或 TempAccountNo,Mycolumn 并且条件是

If (tableII.AccountNo not in table I)

I need to choose TempAccountNo from table II

我需要选择 TempAccountNo from table II

else

I need to choose AccountNo from table II

我需要选择 AccountNo from table II

How can I achieve this.

我怎样才能做到这一点。

回答by zerkms

   SELECT IF(t1.AccountNo IS NULL, t2.TempAccountNo, t2.AccountNo) AS optional_field
     FROM table2 t2
LEFT JOIN t1 ON t1.AccountNo = t2.AccountNo

回答by Er. Nilesh G. Pangul

I have a mysql query for multiple select field using if ... else statements or using case statement. This is described below.

我使用 if ... else 语句或 using case 语句对多个选择字段进行了 mysql 查询。这在下面描述。

  1. MYSQL select using if statement

    select IF('fieldname with condition','if true value','if false value') from table_name where 1;
    
  2. MYSQL select using multiple 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;
    
  1. MYSQL 选择使用 if 语句

    select IF('fieldname with condition','if true value','if false value') from table_name where 1;
    
  2. 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;
    

Try using these queries.

尝试使用这些查询。

回答by Prabhagaran

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

This will help..

这将有助于..