我正在尝试这样做
SELECT
table1.*,
table2.id as t2id
FROM
table1 as t1
INNER JOIN table2 as t2
ON t2.field1 = t1.field2
AND t1.field2 = 'value'
AND IF(SELECT COUNT(*) FROM table2 WHERE id = 10 > 0)
错误提示
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
'SELECT COUNT(*) FROM table2 WHERE id = 10 > 0) LIMIT ' at line 1
我知道错误与 if 条件有关,因为当我删除它时,它起作用了,但我的想法是如果选择不成功,select 将返回一个空值,即它没有在 id 为 10 的表中找到任何内容来自表 2。
最佳答案
您缺少 if 的“then”部分。
它看起来太像 IF (condition, then, else),但你只是在执行条件而没有任何输出。
这样试试:
AND IF((SELECT COUNT(*) FROM table2 WHERE id = 10) > 0, 'true', 'false')
关于mysql - if 条件中的 Select 语句不适用于其他参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16239364/