如何按名称获得相同的Ip计数组?
select
name,count(*) as count
from p
group by name
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `p`
-- ----------------------------
DROP TABLE IF EXISTS `p`;
CREATE TABLE `p` (
`id` int(10) NOT NULL auto_increment,
`name` varchar(200) default NULL,
`ip` varchar(20) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of p
-- ----------------------------
INSERT INTO `p` VALUES ('1', 'jimmy', '1.1.1.1');
INSERT INTO `p` VALUES ('2', 'jimmy', '1.1.1.2');
INSERT INTO `p` VALUES ('3', 'tina', '1.1.1.1');
INSERT INTO `p` VALUES ('4', 'tina', '1.2.3.4');
INSERT INTO `p` VALUES ('6', 'jimmy', '1.1.1.1');
INSERT INTO `p` VALUES ('7', 'jimmy', '1.1.1.3');
INSERT INTO `p` VALUES ('8', 'darcy', '1.1.1.1');
INSERT INTO `p` VALUES ('9', 'darcy', '1.2.1.2');
答案 0 :(得分:2)
你可以试试这个
select
name,
(select count(*) from p as t_detail where t1.ip = t_detail.ip and t1.name = t_detail.name) AS count
from p as t1
group by name
答案 1 :(得分:1)
select name,
count(*) as count,
count(*) - (count(*) - count(distinct ip)) as same_ip_count
from p
group by name