MySQL“如果存在"错误 [英] MySQL 'if exists' error

查看:88
本文介绍了MySQL“如果存在"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MySQL查询中出现错误:

I got a error in my MySQL query:

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

我的问题在哪里?

推荐答案

另一种方法:您也可以使用case when

Another method: You may also use case when

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

* SQLFiddle演示

样品表:

* SQLFiddle Demo

Sample table:

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

这篇关于MySQL“如果存在"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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