MySQL Forums
Forum List  »  Newbie

1242 - Subquery returns more than 1 row
Posted by: Chevy Mark Sunderland
Date: April 02, 2012 08:50AM

Hello.
First of all I must say that I am a newbie when it comes to MySQL.
Here is my problem.
I need update the field `myNumberPreviousYear` of the table `tbl_x` with the value returned from query select count(*) in the table `tbl_z` for single field `myType` and where field `myArea`.
I try this update query and subquery but I have error: 1242 - Subquery returns more than 1 row
Can you help me?
Thanks for your time and hints.
Chevy

mysql> UPDATE `tbl_x` t
JOIN(
	SELECT
		(
			SELECT
				COUNT(*) AS `T`
			FROM
				`tbl_z`
			WHERE
				1
			AND `myDate` BETWEEN MAKEDATE(YEAR(CURDATE()) - 1, 1)
			AND MAKEDATE(YEAR(CURDATE()) - 1, 365)
			AND `myArea` = 'DX'
			GROUP BY
				`myType`
		)q
)x
SET t.myNumberPreviousYear = x.q
WHERE
	1
AND `myArea` = 'DX';
1242 - Subquery returns more than 1 row



mysql> select version();
+---------------------+
| version()           |
+---------------------+
| 5.0.45-community-nt |
+---------------------+
1 row in set




SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `tbl_z`
-- ----------------------------
DROP TABLE IF EXISTS `tbl_z`;
CREATE TABLE `tbl_z` (
  `myId` int(11) NOT NULL auto_increment,
  `myDate` date default NULL,
  `myArea` varchar(4) default NULL,
  `myType` varchar(50) default NULL,
  UNIQUE KEY `myId` (`myId`),
  KEY `myArea` (`myArea`),
  KEY `myType` (`myType`)
) ENGINE=MyISAM AUTO_INCREMENT=209 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of tbl_z
-- ----------------------------
INSERT INTO tbl_z VALUES ('1', null, '2012-03-06', 'DX', 'C');
INSERT INTO tbl_z VALUES ('2', null, '2012-02-17', 'DX', 'E');
INSERT INTO tbl_z VALUES ('3', null, '2012-02-17', 'DX', 'E');
INSERT INTO tbl_z VALUES ('4', null, '2012-02-16', 'DX', 'F');
INSERT INTO tbl_z VALUES ('5', null, '2012-02-06', 'DX', 'F');
INSERT INTO tbl_z VALUES ('6', null, '2012-02-03', 'DX', 'D');
INSERT INTO tbl_z VALUES ('7', null, '2012-02-03', 'DX', 'F');
INSERT INTO tbl_z VALUES ('9', null, '2012-01-31', 'DX', 'A');
INSERT INTO tbl_z VALUES ('10', null, '2012-01-31', 'DX', 'D');





SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `tbl_x`
-- ----------------------------
DROP TABLE IF EXISTS `tbl_x`;
CREATE TABLE `tbl_x` (
  `myType` varchar(255) default NULL,
  `myNumberPreviousYear` int(10) default NULL,
  `myNumberPreviousMonthYear` int(10) default NULL,
  `myNumberCurrentYear` int(10) default NULL,
  `myChange` decimal(10,0) default NULL,
  `myArea` char(6) default NULL,
  `myId` int(10) NOT NULL auto_increment,
  PRIMARY KEY  (`myId`)
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=latin1 ROW_FORMAT=FIXED;

-- ----------------------------
-- Records of tbl_x
-- ----------------------------
INSERT INTO tbl_x VALUES ('A', null, null, null, null, 'DX', '1');
INSERT INTO tbl_x VALUES ('B', null, null, null, null, 'DX', '2');
INSERT INTO tbl_x VALUES ('C', null, null, null, null, 'DX', '3');
INSERT INTO tbl_x VALUES ('D', null, null, null, null, 'DX', '4');
INSERT INTO tbl_x VALUES ('E', null, null, null, null, 'DX', '5');
INSERT INTO tbl_x VALUES ('F', null, null, null, null, 'DX', '6');
INSERT INTO tbl_x VALUES ('G', null, null, null, null, 'DX', '7');
INSERT INTO tbl_x VALUES ('H', null, null, null, null, 'DX', '8');

Options: ReplyQuote


Subject
Written By
Posted
1242 - Subquery returns more than 1 row
April 02, 2012 08:50AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.