【发布时间】: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,这 - 嗯 - 没有意义。条件是什么,如果为真,你想要的值是多少? -
查看原始帖子中的编辑以获取更清晰的描述