MYSQL返回0行

时间:2017-04-23 09:52:54

标签: mysql

用户表:

CREATE TABLE IF NOT EXISTS `user` (
  `id` bigint(200) NOT NULL AUTO_INCREMENT,
  `reg_title` enum('Mr.','Mrs.','Ms.') NOT NULL,
  `reg_first_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  `reg_last_name` varchar(250) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  `reg_email` varchar(500) NOT NULL,
  `reg_password` varchar(500) NOT NULL,
  `fbid` text NOT NULL,
  `secret_question` varchar(1024) NOT NULL,
  `secret_answer` varchar(1024) NOT NULL,
  `company` varchar(1024) NOT NULL,
  `work_phone_number` varchar(1024) NOT NULL,
  `home_phone_number` varchar(1024) NOT NULL,
  `mobile_phone_number` varchar(1024) NOT NULL,
  `code` text NOT NULL,
  `reg_address` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  `reg_no` varchar(1024) NOT NULL,
  `reg_country` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  `reg_postal` varchar(50) NOT NULL,
  `reg_city` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  `reg_state` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  `reg_gender` enum('Male','Female') NOT NULL,
  `reg_verifycode` varchar(50) NOT NULL,
  `reg_active` enum('Yes','No') NOT NULL,
  `type` varchar(100) NOT NULL,
  `token` text NOT NULL,
  `image` varchar(1024) NOT NULL,
  `reg_voucher` varchar(1024) NOT NULL,
  `reg_date` varchar(1024) NOT NULL,
  `user_lat` varchar(1024) NOT NULL,
  `user_long` varchar(1024) NOT NULL,
  `newsletteractive` varchar(10) NOT NULL,
  `showtel` enum('Yes','No') NOT NULL,
  `website` varchar(1024) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `reg_email` (`reg_email`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

INSERT INTO `user` (`id`, `reg_title`, `reg_first_name`, `reg_last_name`, `reg_email`, `reg_password`, `fbid`, `secret_question`, `secret_answer`, `company`, `work_phone_number`, `home_phone_number`, `mobile_phone_number`, `code`, `reg_address`, `reg_no`, `reg_country`, `reg_postal`, `reg_city`, `reg_state`, `reg_gender`, `reg_verifycode`, `reg_active`, `type`, `token`, `image`, `reg_voucher`, `reg_date`, `user_lat`, `user_long`, `newsletteractive`, `showtel`, `website`) VALUES
(1, 'Mr.', 'shah', 'Bonny', 'shahrushabh1996@gmail.com', '$2y$15$NIsMTLvvHEL4rt0vS2p4f.FSyw8Nu3zBl8yBlBtsMixC5USH/AJn.', '', '', '', '', '', '', '', '', 'b/1 rajasthan  Flat', '123', 'India', '380005', 'Ahmedabad', NULL, 'Male', '899884070', 'Yes', '', '', '', '', '2017-04-18', '', '', 'Yes', 'No', ''),
(2, 'Mr.', 'asdad', 'asdd', 'dasdad', '$2y$15$F3PpgfCWRSl0H4Z9QW/b2OqT71j8kKRaSLay9ZQzlkb8Yd0TnOjES', '', '', '', '', '', '', '', '', 'asdad', '', 'Antarctica', '', 'asdad', NULL, 'Male', '', 'Yes', '', '', '', '', '', '', '', 'Yes', 'No', ''),
(3, 'Mr.', 'asdad', 'asdd', 'dasdadasdd', '$2y$15$UehLZxQXgB25HDrJADBUT.uFQ1oQM/1qs2PRoH9woNaBqnRuRNsry', '', '', '', '', '', '', '', '', 'asdad', '', 'Antarctica', '', 'asdad', NULL, 'Male', '', 'Yes', '', '', '', '', '', '', '', 'Yes', 'No', ''),
(4, 'Mr.', 'asdad', 'asdd', 'dasdadas', '$2y$15$70cQ.4gBmcQ8g3ygwm07huhe4nOz//RHzzkCUB6F5sh9isjkXGSdC', '', '', '', '', '', '', '', '', 'asdad', '', 'Antarctica', '', 'asdad', NULL, 'Male', '', 'Yes', '', '', '', '', '', '', '', 'Yes', 'No', ''),
(5, 'Mr.', 'Dhaval', 'Patel', 'dhaval94276@gmail.com', '$2y$15$zgxR4cbGGdu/1tHwcjolrOE28XxcIvUWHEYcJkpuDdMF0akvxD2l2', '', '', '', '', '', '', '', '', 'New Ranip Road', '203', 'India', '382480', 'Ahmedabad', NULL, 'Male', '', 'Yes', '', '', '', '', '', '', '', 'Yes', 'No', ''),
(6, 'Mrs.', 'Shah', 'Bonny', 'shahrushabh2000@gmail.com', '$2y$15$FPwNyTwnWZ09SXuPdRj7Auj5ditoG/xhwMviyTsuY7q9y.fLDmSzC', '', '', '', '', '', '', 'a:3:{i:0;s:10:"9586861237";i:1;s:10:"9429058733";i:2;s:10:"9427508602";}', 'a:5:{i:0;s:2:"91";i:1;s:2:"91";i:2;s:2:"91";i:3;s:12:"Country Code";i:4;s:12:"Country Code";}', 'b/1 rajastahn flat, kabirchowk', '1', 'India', '380005', 'ahmedabad', NULL, 'Male', '', 'Yes', '', '', '', '', '', '', '', 'Yes', 'No', 'www.google.com'),
(7, 'Mr.', 'Vatsal', 'bhatt', 'vatsalbhatt963@gmail.com', '$2y$15$TNN0dI5Z4od.oWUyGlhXtuq1aB9WV5ge0VrK/qlIc/g2v.yoWuP7W', '', '', '', '', '', '', '', '', NULL, '', NULL, '', NULL, NULL, 'Male', '468241391', 'Yes', '', '', '', '', '2017-04-19', '', '', '', 'Yes', '');
宠物桌:

CREATE TABLE IF NOT EXISTS `pet_info` (
  `id` bigint(200) NOT NULL AUTO_INCREMENT,
  `pet_name` varchar(14) NOT NULL,
  `pet_cat` bigint(100) NOT NULL,
  `pet_subcat` bigint(200) NOT NULL,
  `pet_breed` varchar(500) NOT NULL,
  `pet_age` int(100) NOT NULL,
  `pet_age2` int(100) NOT NULL,
  `pet_birthdate` varchar(1024) NOT NULL,
  `pet_sex` varchar(100) NOT NULL,
  `pet_furcolor` varchar(250) NOT NULL,
  `pet_aggg` enum('birth','age') NOT NULL,
  `pet_hidenum` varchar(10) NOT NULL,
  `pet_website` varchar(50) NOT NULL,
  `pet_chipped` varchar(250) NOT NULL,
  `pet_weight` varchar(100) NOT NULL,
  `pet_about` mediumtext NOT NULL,
  `pet_chip` varchar(15) NOT NULL,
  `pet_alive` enum('Alive','Dead') NOT NULL,
  `pet_user_id` bigint(200) NOT NULL,
  `registerdate` date NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;

INSERT INTO `pet_info` (`id`, `pet_name`, `pet_cat`, `pet_subcat`, `pet_breed`, `pet_age`, `pet_age2`, `pet_birthdate`, `pet_sex`, `pet_furcolor`, `pet_aggg`, `pet_hidenum`, `pet_website`, `pet_chipped`, `pet_weight`, `pet_about`, `pet_chip`, `pet_alive`, `pet_user_id`, `registerdate`) VALUES
(5, 'Casper', 1, 0, '254', 0, 0, '08/12/2016', 'Male', 'Cream', 'birth', '461558334', '', 'Yes', '', 'Ti has brown color and good looking.', '123232456656766', 'Alive', 7, '0000-00-00'),
(6, '', 1, 0, '6', 0, 0, '22/02/2017', 'Male', 'Gray', '', '707593403', '', 'Yes', '', 'Hello RJ.', '252546', 'Alive', 6, '0000-00-00'),
(4, '', 2, 0, '2', 0, 0, '', 'Male', 'Black', 'birth', '573864291', '', 'Yes', '', 'Hello Pet.', '123456', 'Alive', 6, '0000-00-00'),
(7, 'scarpy2', 2, 0, '6', 9, 4, '01/03/2017', 'Male', 'Red', 'birth', '24528836', '', 'Yes', '', 'help to scarpy2', '77777777777777', 'Alive', 5, '0000-00-00'),
(10, '', 1, 0, '34', 0, 0, '', 'Male', 'White', 'birth', '952604149', '', 'No', '', 'test', '', 'Alive', 5, '0000-00-00'),
(9, '', 2, 0, '12', 0, 0, '', 'Castrated', 'Brown', 'birth', '14329991', '', 'No', '', 'test', '', 'Alive', 0, '0000-00-00'),
(11, '', 1, 0, '34', 0, 0, '', 'Male', 'White', 'birth', '190164001', '', 'No', '', 'test', '', 'Alive', 5, '0000-00-00'),
(12, '', 1, 0, '34', 0, 0, '', 'Male', 'White', 'birth', '73749768', '', 'No', '', 'test', '', 'Alive', 5, '0000-00-00'),
(13, '', 1, 0, '34', 0, 0, '', 'Male', 'White', 'birth', '726772312', '', 'No', '', 'test', '', 'Alive', 5, '0000-00-00'),
(14, '', 1, 0, '34', 0, 0, '', 'Male', 'White', 'birth', '167617077', '', 'No', '', 'test', '', 'Alive', 5, '0000-00-00');

消息表:

CREATE TABLE IF NOT EXISTS `message` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `comment` text NOT NULL,
  `user_id` int(11) NOT NULL,
  `owner_id` int(11) NOT NULL,
  `pet_id` int(11) NOT NULL,
  `status` enum('replied','seen','not seen') NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

INSERT INTO `message` (`id`, `comment`, `user_id`, `owner_id`, `pet_id`, `status`) VALUES
(4, 'Gello Rushabh mr I seen your pet', 1, 6, 6, 'not seen');

回复表:

CREATE TABLE IF NOT EXISTS `response` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `message_id` int(11) NOT NULL,
  `response` text NOT NULL,
  `status` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

INSERT INTO `response` (`id`, `message_id`, `response`, `status`) VALUES
(3, 1, 'Hello rj', ''),
(5, 3, 'my also', ''),
(6, 2, 'HADOOP FOR BIG DATA', '');

MYSQL查询:

SELECT *, `message`.`status` as `message_status`, `message`.`id` as `message_id`, group_concat(response.response) as response, group_concat(response.id) as response_id, `message`.`user_id` as `sender`,(SELECT CONCAT(`user`.`reg_first_name`, `user`.`reg_last_name`) FROM `user` WHERE `user`.`id` = `message`.`user_id`) as sender_name FROM `message` LEFT JOIN `pet_info` ON `pet_info`.`id` = `message`.`pet_id` LEFT JOIN `response` ON `response`.`message_id` = `message`.`id` LEFT JOIN `user` ON `user`.`id` = `message`.`user_id` WHERE `user_id` = '6' AND `pet_id` = '6' GROUP BY `message`.`id`

我的查询没有返回任何行,任何人都可以帮助我为什么会发生这种情况。以及如何解决此错误?我在Google上搜索了很多,但没有找到任何帮助,所以我在谷歌上询问。谢谢,堆栈社区。

2 个答案:

答案 0 :(得分:0)

尝试替换

(SELECT `user`.`reg_first_name`, `user`.`reg_last_name` 
LEFT JOIN `user` ON `user`.`id` = `message`.`user_id`) as sender_name

(SELECT CONCAT(`user`.`reg_first_name`, `user`.`reg_last_name`) 
FROM `user` WHERE `user`.`id` = `message`.`user_id`) as sender_name

<强>更新

根据编辑问题,我已完全修改了这个答案:

查询不返回任何数据的原因是user_id子句中的WHERE条件,例如:

WHERE `user_id` = '6' AND `pet_id` = '6'

messages表只包含一行且user_id 1,而此查询尝试搜索user_id 6的记录,因此没有数据。

下面应该有效:

SELECT *
FROM `message` 
LEFT JOIN `pet_info` ON `pet_info`.`id` = `message`.`pet_id` 
LEFT JOIN `response` ON `response`.`message_id` = `message`.`id` 
LEFT JOIN `user` ON `user`.`id` = `message`.`user_id` 
WHERE `user_id` = 1 AND `pet_id` = 6

答案 1 :(得分:0)

问题是,在您的内部查询中,您缺少FROM子句,并且您正在尝试获取多个不可能的记录。你应该改变你的查询,如下所示。请注意,没有子查询,因为您不需要它。

SELECT *, 
`message`.`status` as `message_status`, 
`message`.`id` as `message_id`, 
group_concat(response.response) as response, 
group_concat(response.id) as response_id, 
`message`.`user_id` as `sender`,
`user`.`reg_first_name`, 
`user`.`reg_last_name` 
FROM `messages` 
LEFT JOIN `pet_info` ON `pet_info`.`id` = `message`.`pet_id` 
LEFT JOIN `response` ON `response`.`message_id` = `message`.`id` 
LEFT JOIN `user` ON `user`.`id` = `message`.`user_id` 
WHERE `user_id` = '6' 
AND `pet_id` = '6' 
GROUP BY `message`.`id`;
相关问题