Java SQL内部查询-子查询返回超过1行
我收到的错误信息是 错误代码:1242 子查询返回超过1行 解决方案???您的内部查询Java SQL内部查询-子查询返回超过1行,java,mysql,Java,Mysql,我收到的错误信息是 错误代码:1242 子查询返回超过1行 解决方案???您的内部查询 SELECT DISTINCT cmt.topic_id,ctt.description AS topic_name,cmrt.room_id,cmrt.family_id, crt.title AS room_name, ft.family_identifier AS family_name, upt.gender, cmt.postedby_userid, cmt.member_id, ut.pictu
SELECT DISTINCT cmt.topic_id,ctt.description AS topic_name,cmrt.room_id,cmrt.family_id, crt.title AS room_name, ft.family_identifier AS family_name, upt.gender, cmt.postedby_userid, cmt.member_id,
ut.picture_filename AS senderImage,
ut.croppedpicture_filename AS senderCroppedImage,
cmt.image AS imageUrl ,
cmt.message AS caption,
cmrt.user_id,
(SELECT COUNT(id) FROM `conversation_messages_tbl` a ,`conversation_msgreadstatus_tbl` b WHERE a.`message_id` = b.`message_id`
AND a.`topic_id` = b.`topic_id` AND b.`is_read`= 0 AND b.`user_id`!= 27 GROUP BY b.`user_id`) countn
FROM conversation_messages_tbl cmt,
conversation_topics_tbl ctt ,
conversation_msgreadstatus_tbl cmrt,
conversation_rooms_tbl crt, family_tbl ft,
user_profileinformation_tbl upt,
user_tbl ut
WHERE ctt.topic_id=cmt.topic_id AND cmrt.message_id=cmt.message_id
AND upt.user_id=cmt.postedby_userid AND crt.room_id=cmrt.room_id AND ft.family_id=crt.family_id
AND ut.user_id=cmt.postedby_userid AND cmt.message_id=202 GROUP BY cmrt.user_id;
可以返回多行,因为它将返回每个不同用户id的计数,不同于27行(因为group by)。子查询将返回多行,因为您使用了
group by b.user id
,并且查询和子查询之间没有条件
如果我的想法正确,cmrt.user\u id
等于b.user\u id
,您可以向子查询添加如下条件:
(SELECT COUNT(id) FROM `conversation_messages_tbl` a ,`conversation_msgreadstatus_tbl` b WHERE a.`message_id` = b.`message_id` AND a.`topic_id` = b.`topic_id` AND b.`is_read`= 0 AND b.`user_id`!= 27 GROUP BY b.`user_id`) countn
不要运行返回超过1行的子查询。
(SELECT COUNT(id) FROM `conversation_messages_tbl` a
`conversation_msgreadstatus_tbl` b WHERE a.`message_id` = b.`message_id`
AND a.`topic_id` = b.`topic_id` AND b.`is_read`= 0 AND b.`user_id`!= 27
b.`user_id`=cmrt.user_id) countn