Search code examples
mysqldate-formattingmysql-error-1241

Error message "Operand should contain 1 column(s)"


Am trying to select the day and month part for a range of date of a date field in mysql and its given me this error"Operand should contain 1 column(s)". tablename is personal date field name is dob

"SELECT *
FROM personal
WHERE (EXTRACT(MONTH
               FROM dob),
       EXTRACT(DAY
               FROM dob)) BETWEEN '$fromdate' AND '$todate'"

dob is date field and the $fromdate and $todate holds a date input from the screen like "2014-04-30" and "2014-06-30


Solution

  • You need to use CONCAT in order to merge day and month from your dob column and then compare using between clause

    SELECT 
      * 
    FROM
      personal 
    WHERE
    CONCAT(EXTRACT(MONTH FROM dob),'-',EXTRACT(DAY FROM dob))
    BETWEEN '$fromdate'   AND '$todate' 
    

    Or better to use DATE_FORMAT to get the month and day only but make sure you have standard date object stored in column, and the format you provide in DATE_FROMAT must match with the format of your provided parameters

    SELECT 
      * 
    FROM
      personal 
    WHERE
    DATE_FROMAT(dob,'%m-%d')
    BETWEEN '$fromdate'   AND '$todate'