开发者

errorcode:1064 when filtering members based on age [closed]

开发者 https://www.devze.com 2023-04-05 21:51 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly开发者_开发知识库 applicable, visit the help center. Closed 9 years ago.

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

**


  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
0

精彩评论

暂无评论...
验证码 换一张
取 消