使用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
select IF('fieldname with condition','if true value','if false value') from table_name where 1;
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
这将有助于……为什么这是最被接受的答案?它有什么作用?