简体   繁体   English

错误消息“操作数应包含 1 列”

[英]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)".我正在尝试为 mysql 中日期字段的日期范围选择日期和月份部分,它给了我这个错误“操作数应该包含 1 列”。 tablename is personal date field name is dob表名是个人日期字段名是 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 dob 是日期字段, $fromdate 和 $todate 保存从屏幕输入的日期,如“2014-04-30”和“2014-06-30”

Using WHERE clause you can compare multiple values but one each at a time.使用WHERE子句,您可以比较多个值,但一次只能比较一个。

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.在您的声明中WHERE试图同时使用MONTHDAY作为单独的值,逗号分隔但与$fromdate$tdate进行比较,这是不正确的。

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如果$fromdate$todate的格式为mmdd ,则

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

You need to use CONCAT in order to merge day and month from your dob column and then compare using between clause您需要使用CONCAT以便从您的 dob 列中合并日和月,然后使用 between 子句进行比较

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或者最好使用DATE_FORMAT只获取月和日,但要确保您在列中存储了标准日期对象,并且您在DATE_FROMAT中提供的格式必须与您提供的参数的格式相匹配

SELECT 
  * 
FROM
  personal 
WHERE
DATE_FROMAT(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如果您不小心将 SELECT 的 cols 放在括号中,您也会收到此错误

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.它抛出操作数应包含 1 列错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM