MySQL';如果存在';错误

MySQL';如果存在';错误,mysql,exists,Mysql,Exists,我的MySQL查询中出现错误: if not exists(select * from tb_user where user_id=1) then select 'ok' as rul; else select 'not' as rul; end if; 我的问题在哪里?IF语句只能在存储函数中使用。可以使用IF()函数执行所需操作,如下所示: SELECT IF(EXISTS(select * from tb_user where user_id=1), 'ok', 'n

我的MySQL查询中出现错误:

if not exists(select * from tb_user where user_id=1) then
     select 'ok' as rul;
else
     select 'not' as rul;
end if;

我的问题在哪里?

IF语句只能在存储函数中使用。可以使用IF()函数执行所需操作,如下所示:

SELECT IF(EXISTS(select * from tb_user where user_id=1), 'ok', 'not') as rul;

另一种方法:当

Select case
when exists(select * from tb_user where user_id=1)
then 'Ok'
else 'Not'
end
;
* 样本表:

ID  NAME
1   john
2   tim
3   jack
4   rose
查询:将列重命名为
Status

Select case
when exists(select * from table1 where id=1)
then 'Ok'
else 'Not'
end as Status
;
结果:

STATUS
Ok

您可能还需要检查这一点,以便为您找到最佳解决方案。:)
STATUS
Ok