【问题标题】:mysql case statement, some work and some don'tmysql case语句,有些工作有些不行
【发布时间】:2014-04-19 16:14:11
【问题描述】:

我尝试在存储过程中运行它,但问题似乎在于我处理“_registrations”和“_warnings”部分的案例语句。

这个想法是表中的所有条目都有一种注册类型,0 或 1,我想使用参数选择所有注册为 1 或注册为 2 或全部,不管注册的价值如何……

我的尝试,显然失败了

create procedure get_table_information3(_sort character(2), _start int, _page int, _seek varchar(64), _registrations varchar(64), _warnings varchar(64),  _categories varchar(64))
begin   
    select * from Students
    where  
        first_name like coalesce(concat('%', _seek, '%'), first_name) or
        last_name like coalesce(concat('%', _seek, '%'), last_name) or
        email like coalesce(concat('%', _seek, '%'), email) or
        comments like coalesce(concat('%', _seek, '%'), comments)  
            case when _registrations = 0 then and reg = 0,
        case when _registrations = 1 then and reg = 1 end,
        case when _warnings = 0 then and warning = 0,
        case when _warnings = 1 then and warning = 1 end,
        order by
        case when _sort = 'fa' then first_name end asc,
        case when _sort = 'fd' then first_name end desc,
    limit _start, _page;
end

最后 2 个 case 语句有效(我知道,因为在删除 4 个第一个 case 语句时程序运行)。但是,当添加这些时,一切都失败了。

如果知道这一点很重要,我会使用 MariaDB。错误信息:

1064 - 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以了解在 ' 和 reg = 0 端附近使用的正确语法, 当 _registrations = 1 then 并且 reg = 1 end 时,'

我已经阅读了https://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html,但显然有些东西我不明白。

非常感谢您的帮助。

问候, 尼克拉斯


编辑

我也试过了:

create procedure get_table_information3(_sort character(2), _start int, _page int, _seek varchar(64), _registrations varchar(64), _warnings varchar(64), _categories varchar(64)) begin
select * from Students where
first_name like coalesce(concat('%', _seek, '%'), first_name) or last_name like coalesce(concat('%', _seek, '%'), last_name) or email like coalesce(concat('%', _seek, '%'), email) or comments like coalesce(concat('%', _seek, '%'), comments) and case _registrations when 0 then reg = 0, when 1 then reg = 1, else reg in (0,1) end case order by case when _sort = 'fa' then first_name end asc, case when _sort = 'fd' then first_name end desc, limit _start, _page; end


这应该做我想要的,但是“语法错误”虽然我认为我已经从手册中复制了语法......

【问题讨论】:

  • “当 _registrations = 0 然后 reg = 0”的情况看起来不正确。你实际上想用这条线实现什么? “当 _registrations = 0 且 reg = 0 然后 1 else 0 结束时的情况”?
  • 好吧,在编写普通的“select *”语句时,您通常用“and”、“or”等分隔条件。这里不需要吗?
  • 它是 CASE <condition> THEN <value if true> ELSE <value if false> END,但在这种情况下,您使用的是 THEN AND,这 - 嗯 - 没有意义。条件是什么,如果为真,你想要的值是多少?
  • 查看原始帖子中的编辑以获取更清晰的描述

标签: mysql case mariadb


【解决方案1】:

不确定这是否准确地捕捉到了你想要的逻辑,但它应该会给你一个想法;

一个CASE 语句检查reg=0 是否_registrations=0 应该是这样的;

case when _registrations = 0 then reg = 0 ELSE 1 END

如果_registrations = 0,则返回reg=0的值,否则为1(真),即如果_registrations0,则表达式始终为真。

这使得整个查询类似于;

create procedure get_table_information3(_sort character(2), _start int, _page int, _seek varchar(64), _registrations varchar(64), _warnings varchar(64),  _categories varchar(64))
begin   
    select * from Students
    where  
       (first_name like coalesce(concat('%', _seek, '%'), first_name) or
        last_name like coalesce(concat('%', _seek, '%'), last_name) or
        email like coalesce(concat('%', _seek, '%'), email) or
        comments like coalesce(concat('%', _seek, '%'), comments)) AND 
        case when _registrations = 0 then reg = 0 ELSE 1 END AND
        case when _registrations = 1 then reg = 1 ELSE 1 END AND
        case when _warnings = 0 then warning = 0 ELSE 1 END AND
        case when _warnings = 1 then warning = 1 ELSE 1 END
        order by
        case when _sort = 'fa' then first_name end asc,
        case when _sort = 'fd' then first_name end desc
    limit _start, _page;
end
//

【讨论】:

  • 感谢您的帮助,我得到的错误减少了,但它仍然无法运行。早些时候它抱怨案例语句中的语法错误,但现在它说“1064 - 您的 SQL 语法有错误;请查看与您的 MariaDB 服务器版本相对应的手册,以在第 28 行的 '' 附近使用正确的语法”。第 28 行是“limit _start, _page;”行,但我看不出那里应该有什么问题。 (我在程序的这一部分之前还有一些代码,但是那个没有引起任何错误,所以我在这里不包括在内)
  • 我解决了,文件中的一个愚蠢的错字我有导入它的程序导致最后一个错误。谢谢。
猜你喜欢
  • 2012-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
  • 2013-04-13
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多