【问题标题】:MySQL run query if previous query is not null如果先前的查询不为空,则 MySQL 运行查询
【发布时间】:2019-05-22 17:14:38
【问题描述】:

我正在尝试运行两个查询,其中只有第一个不为空时才会运行第二个查询。比如:

    if((select * from abc where id =1)!=null)
      select * from cde
    else exit;

执行此类操作的正确方法是什么?

【问题讨论】:

    标签: mysql if-statement stored-procedures


    【解决方案1】:

    使用存在条件

    只有select 1 from abc where id =1返回至少一条记录select * from cde才会被执行

     select * from cde
        where exists (select 1 from abc where id =1 )
    

    如果你需要执行其他语句,你可以使用类似下面的语句

      if exits (SELECT 1 from abc where id =1) then
       --  select a into var_x from cde...
       --  upddate ...
     else
       --  
     end if;
    

    【讨论】:

    • 如果退出(从 abc 中选择 *),是否可以像 call procedure() 那样调用一个过程?
    • 是的,但我不确定,我刚刚编辑了我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-02
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多