根据年龄过滤成员时出现错误代码:1064
我想要获取年龄在这两个值之间的人(假设我想过滤掉年龄在 18 到 24 岁之间的人)。
这是成员表结构:
member_id
member_firstname
member_dob(datatype is string) (values like 1987-09-08,1976-09-08,.....)
我使用 mysql 作为我的数据库......
SELECT members.member_Id,
members.member_Lastname as 'Last Name',
members.member_Firstname as 'First Name',
members.member_Postcode as 'Post Code',
members.member_Reference as Reference,
members.member_CardNum as 'Card Number',
members.member_IsBiometric as Biometric,
members.member_DOB as DoB,
mshiptypes.mshipType_Name as Membership,
mshipstatustypes.mshipStatusType_Name as Status,
membertomships.memberToMship_EndDate as Expiry
FROM members
INNER JOIN membertomships ON membertomships.member_Id = members.member_Id
INNER JOIN mshipoptions ON mshipoptions.mshipOption_Id = membertomships.mshipOption_Id
INNER JOIN mshiptypes ON mshiptypes.mshipType_Id = mshipoptions.mshipType_Id
INNER JOIN mshipstatustypes ON mshipStatusTypes.mshipStatusType_Id = membertomships.mshipStatusType_Id
WHERE memberToMship_EndDate BETWEEN '2011-09-24' AND '2011-09-30'
AND members.member_active LIKE 'y%'
AND (YEAR(CURDATE(members.member_Dob))-YEAR()) - (RIGHT(CURDATE(),5)< RIGHT(member_Dob,5)) BETWEEN '21' AND '102'
ORDER BY members.member_Lastname
出现这样的错误......
Error Code: 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 'member_Dob))-YEAR()) - (RIGHT(CURDATE(),5)< RIGHT(member_Dob,5)) BETWEEN '21' AN' at line 19
修改:
SELECT members.member_Id,
members.member_Lastname as 'Last Name',
members.member_Firstname as 'First Name',
members.member_Postcode as 'Post Code',
members.member_Reference as Reference,
members.member_CardNum as 'Card Number',
members.member_IsBiometric as Biometric,
members.member_DOB as DoB,
mshiptypes.mshipType_Name as Membership,
mshipstatustypes.mshipStatusType_Name as Status,
membertomships.memberToMship_EndDate as Expiry
FROM members
INNER JOIN membertomships ON membertomships.member_Id = members.member_Id
INNER JOIN mshipoptions ON mshipoptions.mshipOption_Id = membertomships.mshipOption_Id
INNER JOIN mshiptypes ON mshiptypes.mshipType_Id = mshipoptions.mshipType_Id
INNER JOIN mshipstatustypes ON mshipStatusTypes.mshipStatusType_Id = membertomships.mshipStatusType_Id
WHERE memberToMship_EndDate BETWEEN '2011-10-01' AND '2011-10-16'
AND members.member_active LIKE 'y%'
AND (YEAR(CURDATE())-YEAR(member_Dob)
- (RIGHT(CURDATE(),5)<RIGHT(member_Dob,5)) BETWEEN '18' AND '109'
ORDER BY members.member_Lastname
error:
**
Error Code: 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 'ORDER BY members.member_Lastname' at line 21
**
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
YEAR()
接受一个date
参数CURDATE()
不带任何参数。将该
AND
子句更改为:请注意,您可能不需要
STR_TO_DATE
如果您将其存储为 YYYY-MM-DD 但为了安全起见,我无论如何都会使用它。下面的条件也应该有效:YEAR()
takes adate
argumentCURDATE()
takes no arguments.Change that
AND
clause to:Note that you might not need
STR_TO_DATE
if you have it stored as YYYY-MM-DD but I would use it anyways to be on the safe side. The condition below should also work: