使用if else条件选择mysql中的列? [英] use the If else condition for selecting the column in mysql?

查看:92
本文介绍了使用if else条件选择mysql中的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个这样的表.两个都是单独的表

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

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,条件是

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

If (tableII.AccountNo not in table I)

我需要选择TempAccountNo from table II

else

我需要选择AccountNo from table II

我如何实现这一目标.

推荐答案

   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 else条件选择mysql中的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆