0 0

sql 查询5

id  name  images
1   张三  http://iamges/123,
2   张三  http://baidu/528,http://sina/456,
3   李四  http://qq/852,

怎么查询出显示结果如下:

name            images                                   count
张三  http://iamges/123,http://baidu/528,http://sina/456,  3
李四   http://qq/852,                                      1

问题补充:如果是如下呢:
id  name  images
1   张三  http://iamges/123
2   张三  http://baidu/528,http://sina/456
3   李四  http://baidu/528,http://sina/456
4   李四  http://qq/852
2012年9月07日 16:43

6个答案 按时间排序 按投票排序

0 0

Oracle中可以借助wm_concat函数来拼接,count计算就可以算拼接后中间逗号出现的次数,所以可以这样实现:

select t.name,
       wm_concat(rtrim(t.images, ',')) images,
       length(wm_concat(rtrim(t.images, ','))) -
       length(replace(wm_concat(rtrim(t.images, ',')), ',')) count
  from WHISKY t
 group by t.name



我试验后查询的结果:
张三 http://iamges/123,http://baidu/528,http://sina/456, 3
李四 http://qq/852, 1

2012年9月07日 17:01
0 0

这个要读取出来多好。。。放如数据库还真有点麻烦
Map。。。

2012年9月07日 16:56
0 0

mysql

select name, group_concat(images separator ''),count(*) from abc group by name;



1   张三  http://iamges/123,
2   张三  http://baidu/528,http://sina/456,

无法统计出3 。因为mysql没有分割字符串的默认函数,,需要自己定义

2012年9月07日 16:51
0 0

sql语句估计挺难写的(不知道能不能做到,或者是否有性能问题)
我以前的做法是先写一个普通的select语句,然后在后台代码中做统计、拼接。

2012年9月07日 16:49
0 1

我刚做了个demo;
show sql

/*
MySQL Data Transfer
Source Host: 127.0.0.1
Source Database: test
Target Host: 127.0.0.1
Target Database: test
Date: 9/7/2012 7:17:30 PM
*/

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for demo
-- ----------------------------
CREATE TABLE `demo` (
  `id` int(2) NOT NULL DEFAULT '0',
  `url` varchar(200) DEFAULT NULL,
  `name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- ----------------------------
-- View structure for demo1
-- ----------------------------
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `demo1` AS select `demo`.`name` AS `name`,group_concat(`demo`.`url` separator ',') AS `myurl` from `demo` group by `demo`.`name`;

-- ----------------------------
-- Function structure for func_get_split_string_total
-- ----------------------------
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` FUNCTION `func_get_split_string_total`(
f_string varchar(1000),f_delimiter varchar(5)
) RETURNS int(11)
BEGIN
  declare returnInt int(11);
  if length(f_delimiter)=2  then
     return 1+(length(f_string) - length(replace(f_string,f_delimiter,'')))/2;
  else    
     return 1+(length(f_string) - length(replace(f_string,f_delimiter,'')));
  end if;
end;;
DELIMITER ;

-- ----------------------------
-- Records 
-- ----------------------------
INSERT INTO `demo` VALUES ('1', 'http://iamges/123', 'demo1');
INSERT INTO `demo` VALUES ('2', ' http://baidu/528,http://sina/456', 'demo1');
INSERT INTO `demo` VALUES ('3', 'http://qq/852', 'demo2');


select name,func_get_split_string_total(myurl,',') from demo1;

2012年9月07日 19:19
0 1

自己和自己join,消除重复,再count

2012年9月07日 17:10

相关推荐

Global site tag (gtag.js) - Google Analytics