SQL IF 语句中的正则表达式
我知道我可以创建基本的比较触发器(见下文)
CREATE TRIGGER HospitalCheck
BEFORE INSERT ON Hospital
FOR EACH ROW
BEGIN
IF NEW.HospitalID > 9999 THEN
call fail('HOSPITAL CODE INVALID');
END IF;
END
我将如何使用仅允许数字的正则表达式? (而不是>9999) (相当于 SELECT 要检查的字符串 REGEXP '^[0-9]+$')
我尝试过:
IF NEW.HospitalID REGEX '^[0-9]+$' THEN
call fail('HOSPITAL CODE INVALID');
END IF;
但我得到
:错误:--> #1064 - 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 'REGEX '^[0-9]+$' 附近使用的正确语法,然后调用失败('医院代码无效');第 5 行 END IF'
I know I can create basic comparison triggers (see below)
CREATE TRIGGER HospitalCheck
BEFORE INSERT ON Hospital
FOR EACH ROW
BEGIN
IF NEW.HospitalID > 9999 THEN
call fail('HOSPITAL CODE INVALID');
END IF;
END
How would I go about using a regular expression that only allowed numbers? (instead of the >9999)
(the equivalent of SELECT string to check REGEXP '^[0-9]+$')
I tried:
IF NEW.HospitalID REGEX '^[0-9]+
But i get
: ERROR : --> #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'REGEX '^[0-9]+$' THEN call fail('HOSPITAL CODE INVALID'); END IF' at line 5
THEN
call fail('HOSPITAL CODE INVALID');
END IF;
But i get
: ERROR : --> #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'REGEX '^[0-9]+$' THEN call fail('HOSPITAL CODE INVALID'); END IF' at line 5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NOT_REGEXP 函数 - 请参阅文档 http://dev.mysql.com/doc /refman/5.1/en/regexp.html
NOT_REGEXP function - see the docs http://dev.mysql.com/doc/refman/5.1/en/regexp.html
只需更改正则表达式
说明
简而言之,这个正则表达式检查 HospitalID 变量,如果发现 HospitalID 变量包含数字以外的字符,则调用失败函数。
Just change the regular expression
Explaination
So in short this regular expression checks the HospitalID variable and if it found that the HospitalID variable had other than numeric characters it call fail function.
IF NOT 列名 REGEXP '^[0-9]+$' THEN
IF NOT column_name REGEXP '^[0-9]+$' THEN