2017-04-23 124 views
0

用戶表:MYSQL返回0行

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', '[email protected]', '$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', '[email protected]', '$2y$15$zgxR4cbGGdu/1tHwcjolrOE28XxcIvUWHEYcJkpuDdMF0akvxD2l2', '', '', '', '', '', '', '', '', 'New Ranip Road', '203', 'India', '382480', 'Ahmedabad', NULL, 'Male', '', 'Yes', '', '', '', '', '', '', '', 'Yes', 'No', ''), 
(6, 'Mrs.', 'Shah', 'Bonny', '[email protected]', '$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', '[email protected]', '$2y$15$TNN0dI5Z4od.oWUyGlhXtuq1aB9WV5ge0VrK/qlIc/g2v.yoWuP7W', '', '', '', '', '', '', '', '', NULL, '', NULL, '', NULL, NULL, 'Male', '468241391', 'Yes', '', '', '', '', '2017-04-19', '', '', '', 'Yes', ''); 

pet表:

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` 

我的查詢不返回任何一行任何人都可以請幫我,爲什麼它的發生。我該如何解決這個錯誤?我在谷歌搜索了很多,但沒有發現有用的東西,所以我在谷歌問。謝謝,堆棧社區。

+0

您從條款 – splash58

+0

中省略您的意思是? –

+0

SELECT'user'.'reg_first_name'','user'.'reg_last_name' ** FROM TABLE ** LEFT JOIN – splash58

回答

0

嘗試用

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

更換

(SELECT `user`.`reg_first_name`, `user`.`reg_last_name` 
LEFT JOIN `user` ON `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 
+0

IT RETURNING 0 ROWS –

+0

那麼,這意味着'messages'和'pet_info'之間沒有匹配的行,或者您加入了錯誤的列。 –

+0

匹配行存在 –

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`; 
+0

請檢查我更新的問題 –