标签:ima join HERE 测试数据 phone primary 三级 结果 arc
1)首先是一个简单的三级分销(邀请与被邀请),表结构是酱紫的
CREATE TABLE `d_user_invite` ( `invite_id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL DEFAULT ‘0‘, `phone` varchar(50) DEFAULT NULL, `invite_user_id` int(11) NOT NULL DEFAULT ‘0‘, `invite_user_phone` varchar(50) DEFAULT NULL, `add_date_time` int(11) NOT NULL, PRIMARY KEY (`invite_id`), KEY `ind_invite_userlD` (`user_id`) USING BTREE, KEY `ind_invite_inviteUserID` (`invite_user_id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2)插入几条测试数据
3)然后 sql 是酱紫的(自己 join 自己,同表自 join)
SELECT `a`.`user_id` AS `uid`, `a`.`phone`, `a`.`invite_user_id` AS `suid`, `a`.`invite_user_phone` AS `sphone`, `b`.`invite_user_id` AS `ssuid`, `b`.`invite_user_phone` AS `ssphone` FROM `d_user_invite` `a` LEFT JOIN `d_user_invite` `b` ON `b`.`user_id` = `a`.`invite_user_id` WHERE `a`.`user_id` <> `b`.`invite_user_id`
4)查询结果
还是觉得蛮神奇,蛮强大的,记录一下~
标签:ima join HERE 测试数据 phone primary 三级 结果 arc
原文地址:https://www.cnblogs.com/tujia/p/9797229.html