根据年龄过滤成员时出现错误代码:1064

发布于 2024-12-05 05:51:11 字数 3328 浏览 1 评论 0 原文

我想要获取年龄在这两个值之间的人(假设我想过滤掉年龄在 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

**

I want to get the people whose age is between these two values (suppose for example I want to filter out the people aged between 18 and 24).

This is the Member table structure:

 member_id 
 member_firstname 
 member_dob(datatype is string) (values like 1987-09-08,1976-09-08,.....)

i am using mysql as my database.....

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

got an error like this.....

     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

modified :

               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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吲‖鸣 2024-12-12 05:51:11
  1. YEAR() 接受一个 date 参数
  2. CURDATE() 不带任何参数。

将该 AND 子句更改为:

AND (YEAR(CURDATE())-YEAR(STR_TO_DATE(member_Dob, '%Y-%m-%d'))
     - (RIGHT(CURDATE(),5)<RIGHT(member_Dob,5))) BETWEEN 21 AND 102

请注意,您可能不需要 STR_TO_DATE 如果您将其存储为 YYYY-MM-DD 但为了安全起见,我无论如何都会使用它。下面的条件也应该有效:

AND (YEAR(CURDATE())-YEAR(member_Dob)
     - (RIGHT(CURDATE(),5)<RIGHT(member_Dob,5))) BETWEEN 21 AND 102
  1. YEAR() takes a date argument
  2. CURDATE() takes no arguments.

Change that AND clause to:

AND (YEAR(CURDATE())-YEAR(STR_TO_DATE(member_Dob, '%Y-%m-%d'))
     - (RIGHT(CURDATE(),5)<RIGHT(member_Dob,5))) BETWEEN 21 AND 102

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:

AND (YEAR(CURDATE())-YEAR(member_Dob)
     - (RIGHT(CURDATE(),5)<RIGHT(member_Dob,5))) BETWEEN 21 AND 102
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文