Question

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

Was it helpful?

Solution 2

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' 

OTHER TIPS

Using WHERE clause you can compare multiple values but one each at a time.

In your statement WHERE is trying to use both MONTH and DAY as separate values comma separated but comparing with $fromdate and $tdate, which is not correct.

And hence is the error:

Operand should contain 1 column(s)

You may be looking for a solution like this:

If $fromdate and $todate are in the format of mmdd, then

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

Just in case anyone else runs across this error (I fought with it for hours before realizing my mistake)

You will also get this error if you accidentally put the cols for a SELECT in parenthesis

So if you are in a hurry and cut and paste your field list and type...

INSERT INTO mytablename(col1, col2, col3)
SELECT (col1, col2, col3) FROM anothertable WHERE col4 = 1;

instead of 
INSERT INTO mytablename(col1, col2, col3)
SELECT col1, col2, col3 FROM anothertable WHERE col4 = 1;

it throws the Operand should contain 1 column(s) error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top