您好我有一个名为tbl_user的表,我想要检索父节点的所有子节点。
每个父ID都分别有左右节点。
我的表格结构和数据
-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Sep 19, 2017 at 08:29 PM
-- Server version: 5.6.17
-- PHP Version: 5.5.12
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `mlm`
--
-- --------------------------------------------------------
--
-- Table structure for table `tbl_user`
--
CREATE TABLE IF NOT EXISTS `tbl_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` varchar(100) DEFAULT NULL,
`full_name` varchar(100) DEFAULT NULL,
`display_name` varchar(100) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`mobile` varchar(100) DEFAULT NULL,
`password` varchar(100) DEFAULT NULL,
`address` text,
`country_id` int(11) DEFAULT NULL,
`state_id` int(11) DEFAULT NULL,
`city_id` int(11) DEFAULT NULL,
`parent_id` varchar(100) DEFAULT NULL,
`user_position` varchar(100) DEFAULT NULL,
`sponser_id` varchar(100) DEFAULT NULL,
`plan_id` int(10) NOT NULL,
`status` varchar(100) DEFAULT NULL,
`user_created` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=30 ;
--
-- Dumping data for table `tbl_user`
--
INSERT INTO `tbl_user` (`id`, `user_id`, `full_name`, `display_name`, `email`, `mobile`, `password`, `address`, `country_id`, `state_id`, `city_id`, `parent_id`, `user_position`, `sponser_id`, `plan_id`, `status`, `user_created`) VALUES
(18, 'OR201701', 'Chita Ranjan Mirdha', 'Chitta', 'chitta@gmail.com', '9861180448', '317419d46c44c40b99f7351739dbec74', NULL, 99, 24, 719, NULL, 'center', NULL, 1, 'active', '2017-09-17'),
(19, 'OR317639', 'Binayak Das', 'Binayak', 'talk2binayak@gmail.com', '9861180448', '317419d46c44c40b99f7351739dbec74', NULL, 99, 11, 222, 'OR201701', 'left', 'OR201701', 1, 'Pending', '2017-09-17'),
(20, 'OR480484', 'Soumya Ranjan Das', 'Soumya', 'soumya002000@gmail.com', '9439149608', '317419d46c44c40b99f7351739dbec74', NULL, 99, 24, 722, 'OR201701', 'right', 'OR201701', 1, 'active', '2017-09-20'),
(23, 'OR594192', 'Sidharth Das', 'Sidharth', 'sidharth@gmail.com', '9861180448', '317419d46c44c40b99f7351739dbec74', NULL, 99, 3, 85, 'OR480484', 'left', 'OR201701', 2, 'Pending', '2017-09-17'),
(24, 'OR355157', 'Rohit Kerketta', 'Rohit', 'rohit@gmail.com', '9861180448', '74b87337454200d4d33f80c4663dc5e5', NULL, 99, 20, 693, 'OR480484', 'right', 'OR201701', 1, 'Pending', '2017-09-17'),
(25, 'OR435604', 'Jabesh Ghanta', 'Jabesh', 'jabesh@gmail.com', '9861180448', '74b87337454200d4d33f80c4663dc5e5', NULL, 99, 24, 719, 'OR317639', 'left', 'OR201701', 2, 'Pending', '2017-09-17'),
(26, 'OR539892', 'Abhishek Behera', 'Daddu', 'abhishek@gmail.com', '9861180448', '11ddbaf3386aea1f2974eee984542152', NULL, 99, 12, 281, 'OR317639', 'right', 'OR201701', 1, 'Pending', '2017-09-18'),
(27, 'OR776071', 'Lalatendu Das', 'LULU', 'talk2lalatendu@gmail.com', '9861180488', '8f60c8102d29fcd525162d02eed4566b', NULL, 99, 11, 209, 'OR355157', 'left', 'OR355157', 1, 'Pending', '2017-09-18'),
(28, 'OR119885', 'Jyotirmaya Das', 'Tikana', 'jmd@gmail.com', '9861180448', '8f60c8102d29fcd525162d02eed4566b', NULL, 99, 5, 112, 'OR355157', 'right', 'OR355157', 1, 'Pending', '2017-09-20'),
(29, 'OR135842', 'Ashish Kumar Samantray', 'Asish', 'asish.samantray@gmail.com', '9861180448', 'decc8686654b465e5313259325149a86', NULL, 99, 24, 716, 'OR435604', 'left', 'OR355157', 2, 'Pending', '2017-09-19');
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
我已经尝试了很多东西来获取用户登录的所有子节点意味着登录用户是父节点。
我有user_id有parent_id。一旦将数据导入mysql,您就可以了解数据和结构。
这是我数据的二进制结构。但我希望数据也在表格中 如下所示
根据我目前的查询,我上面给出的是显示6个结果。但是查询应该按照二叉树显示9个结果。
我认为解释得很好。如果有任何疑问请询问。
答案 0 :(得分:0)
尝试此查询。我想,这对你有用。
SELECT * FROM tbl_user WHERE parent_id ='OR201701' 联盟 选择 * FROM tbl_user WHERE parent_id IN (SELECT user_id FROM tbl_user WHERE parent_id ='OR201701')