SELECT m.id,count(*) as qq FROM xmx_users m LEFT JOIN xmx_users n on m.id = n.recommend_id GROUP BY id
这条sql ,查询出来的结果即使没有符合条件的也显示数量为1。
表:
CREATE TABLE IF NOT EXISTS `xmx_users ` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`recommend_id` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
测试数据:
INSERT INTO `xmx_users ` (`id`,`recommend_id`) VALUES
(2, 0),
(3, 2),
(5, 2),
(6, 0),
(7, 5),
(8, 0),
(9, 0),
(10, 0),
(11,5),
(13, 7);
仙士可
zllo