我尝试在存储过程中运行它,但问题似乎是我的case语句处理“_registrations”和“_warnings”部分。
这个想法是表中的所有条目都有一个注册类型,0或1,我想用一个参数选择所有注册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-statement有效(我知道,因为在删除4个第一个case语句时该过程运行)。然而,当添加这些,一切都很糟糕。
如果知道这一点很重要,我会使用MariaDB。错误讯息:
1064 - 您的SQL语法出错;检查与MariaDB服务器版本对应的手册,以便在'和reg = 0 end附近使用正确的语法, 例如_registrations = 1然后reg = 1结束,'
我已阅读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
应该做我想要的,但是“语法错误”虽然我认为我已经复制了手册中的语法......
答案 0 :(得分:0)
不确定这是否完全捕获了您想要的逻辑,但它应该给您一个想法;
CASE
语句,检查reg=0
是否应该_registrations=0
;
case when _registrations = 0 then reg = 0 ELSE 1 END
如果_registrations = 0
,则会返回reg=0
的值,否则为1(真),即_registrations
<> 0,表达式始终为true。
这使得整个查询就像;
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
//