简体   繁体   English

是什么导致错误(操作数应包含1列)

[英]what is causing error (operands should contain 1 column)

I am trying to set myItemId so that I can use it in the concat query. 我试图设置myItemId,以便我可以在concat查询中使用它。 Everything works fine until I add this row 一切正常,直到我添加这一行

  SET myItemID = (SELECT * FROM items i  WHERE i.name LIKE '%KW PRE FREE COOLING%');

It then gives me an error of Operand should contain 1 column(s) 然后它给我一个操作数应该包含1列的错误

Here is the query that I am working with 这是我正在使用的查询

CREATE PROCEDURE reportFreeCoolingTempTable (
  IN fromDate VARCHAR (50),
  IN toDate   VARCHAR (50),
  IN timeZone VARCHAR (50)
)
BEGIN
  DECLARE startDate VARCHAR (50);
  DECLARE endDate   VARCHAR (50);
  DECLARE mylogID   INT;
  DECLARE myItemID  int;

  SET startDate = FROM_UNIXTIME(fromDate/1000);
  SET endDate   = FROM_UNIXTIME(toDate/1000);
  SET mylogID   = (SELECT logID FROM logs WHERE details LIKE 'FCT%' LIMIT 1);
  SET myItemID = (SELECT * FROM items i  WHERE i.name LIKE '%KW PRE FREE COOLING%');

  SET @sql = NULL;

  SET @sql = NULL;
  SET @sql = CONCAT(
  'SELECT @row:=@row+1 as rownum,
       a.logid ,   
       L1.recordId,
       L2.recordId as next_recordId,
       L1.completed,
       L2.completed as next_completed,
       L1.activityId,
       L2.activityId as next_activityId,
       IF(L1.activityId = L2.activityId,1,NULL) as isError,                           
       TIME_TO_SEC(TIMEDIFF(L2.completed, L1.completed)) / 3600 AS coolingHours,
       ((L1.item31985 - L1.item31987) * (time_to_sec(timediff(L2.completed, L1.completed)))) / 3600  AS kwDifference,
     ((L1.item31985 - L1.item31987) * (substr(l.details, instr(l.details , '':'' ) +1))) AS cost,
    ( (((L1.item31985 - L1.item31987) * (substr(l.details, instr(l.details , '':'' ) +1)))
    *(time_to_sec(timediff(L2.completed, L1.completed)) / 3600))) AS costT,
     time_to_sec(timediff(''', endDate, ''', ''', startDate, ''')) / 3600 AS totalTimeRange,
    CONVERT_TZ(''', startDate, ''', ''UTC'', ''', timeZone, ''') AS startingDate, 
    CONVERT_TZ(''', endDate, ''', ''UTC'', ''', timeZone, ''') AS endingDate,
    DATABASE() AS databaseName

FROM
    (SELECT @row:=0)R,
    (SELECT T1.completed,
       (SELECT MIN(completed)
         FROM log1644
         WHERE completed > T1.completed) AS next_completed
      FROM log',mylogID, ' T1
      ORDER BY T1.completed
     )TimeOrder
        LEFT JOIN log', mylogID, ' L1 ON (L1.completed = TimeOrder.completed)
        LEFT JOIN log', mylogID, ' L2 ON (L2.completed = TimeOrder.next_completed)
        LEFT JOIN activities a ON L1.activityId = a.activityId
        LEFT JOIN logs l ON a.logId = l.logId
        Left Join items i ON l.logId = i.logId AND i.name LIKE ''%KW%''
    WHERE i.itemID = 31985  
        AND L1.completed BETWEEN ''', startDate, ''' AND ''', endDate, '''
ORDER BY L1.completed');



 PREPARE stmt FROM @sql;
 EXECUTE stmt;
 DEALLOCATE PREPARE stmt;
END //


DELIMITER ;

Error itself explains (operands should contain 1 column) you need to select the single column from the query in order to set myItemID ,you are selecting all the columns from the items try this 错误本身解释(operands should contain 1 column)您需要从查询中选择单列以设置myItemID ,您要从items中选择所有列尝试此操作

SET myItemID = (SELECT id FROM items i  WHERE i.name LIKE '%KW PRE FREE COOLING%' LIMIT 1 );

I assume the you need to set the myItemID to the id column from items where you conditions matches.i have also added LIMIT 1 in order to avoid the error of subquery should return one result 我假设您需要将myItemID设置为您条件匹配的项目的id列。我还添加了LIMIT 1以避免子查询的错误应该返回一个结果

The error is caused because the SET statement expects a single value to be returned from your subquery. 导致该错误的原因是SET语句需要从子查询返回单个值。 Not only can it return multiple values (SELECT *), but it can potentially return multiple rows. 它不仅可以返回多个值(SELECT *),而且可以返回多行。 Change your query to specify just the single column from your subquery that you want to assign to myItemId, and ensure that it can return only 1 row - like this: 更改查询以仅指定要分配给myItemId的子查询中的单个列,并确保它只能返回1行 - 如下所示:

SET myItemID = (SELECT TOP 1 [itemIdColumnName] FROM items i  WHERE i.name LIKE '%KW PRE FREE COOLING%');

The 'operand' in your case is "myItemID". 你的情况下的'操作数'是“myItemID”。 It can only hold ONE value. 它只能容纳一个值。 Your SELECT statement returns all the rows in the table (multiple columns). SELECT语句返回表中的所有行(多列)。 You need to select only the 1 column that represents the ID you are trying to obtain. 您只需选择代表您尝试获取的ID的1列。

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

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