实际上,我有一个名为users的表,我必须提取特定用户的子级和孙级用户,就像我试图获取ID为44的用户的子级和孙级用户一样,但我尝试了此查询,但这给了我一些额外的用户,即使他们不是用户44的子代或子代:
SELECT * FROM `users` WHERE `parent_id`='44' OR `parent_id` IN (SELECT `id` FROM `users` WHERE `parent_id`='44' OR `parent_id` IN (SELECT `id` AS 'curret_id' FROM `users` WHERE `parent_id`='44' OR `parent_id` IN (SELECT `id` FROM `users` WHERE `parent_id`='curret_id')))
如果有帮助,这里是我的表的CREATE TABLE语法:
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`join_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`fname` varchar(250) NOT NULL,
`lname` varchar(250) NOT NULL,
`email` varchar(250) NOT NULL,
`phone` varchar(250) NOT NULL,
`dob` date NOT NULL,
`user_type` varchar(250) NOT NULL,
`parent_id` int(11) NOT NULL,
`status` varchar(250) NOT NULL,
) ENGINE=MyISAM DEFAULT CHARSET=latin1;