给定:
t1{id,type}
t2{type,table1_id}
我正在使用这个:
SELECT IF(t1.type IS NULL, 'some default', t1.type) as ret from t1
我想做这样的事情:
SELECT IF(
t1.type IS NULL,
IF(
(SELECT t2.type FROM t2 WHERE t2.table1_id=t1.id LIMIT 1) IS NOT NULL,
table2.type,
'some defaults'
),
t1.type
) as ret from table1
最佳答案
这个——
SELECT IF(
t1.type IS NULL,
IF(
(SELECT t2.type FROM t2
WHERE t2.table1_id=t1.id LIMIT 1)
IS NOT NULL,
t2.type,
'some defaults'),
t1.type
) as ret from t1, t2 where t1.id = t2.table1_id
好像work .
关于mysql - IF 内的子查询作为具有空值的列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13693936/