obj_id path type group_num
------ ------------- ------ -----------
8207 /images/as.jpg S 0
8207 /images/al.jpg L 0
8207 /images/bs.jpg S 1
8207 /images/bl.jpg L 1
8207 /images/cs.jpg S 2
8207 /images/cl.jpg L 2
想得到以下效果,用sql语句应该怎么实现?mysql数据库
obj_id small_img large_img group_num
8207 /images/as.jpg /images/al.jpg 0
8207 /images/bs.jpg /images/bl.jpg 1
8207 /images/cs.jpg /images/cl.jpg 2
下面是建表和造数据语句 , type表示图片类型,'S'是小图片,'L'是大图片
CREATE TABLE `t_images` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`obj_id` INT(11) DEFAULT NULL,
`path` VARCHAR(255) DEFAULT NULL,
`type` VARCHAR(2) DEFAULT NULL,
`group_num` INT(1) DEFAULT NULL,
PRIMARY KEY (`id`)
)
INSERT INTO t_images (obj_id,path,TYPE,group_num)
VALUES
(8207,'/images/as.jpg','S',0),(8207,'/images/al.jpg','L',0)
,(8207,'/images/bs.jpg','S',1),(8207,'/images/bl.jpg','L',1)
,(8207,'/images/cs.jpg','S',2),(8207,'/images/cl.jpg','L',2)
不知道 obj_id 是什么含义,看看下面的SQL,是否满足你的要求:
select max(obj_id) as obj_id, max(small_img) as small_img, max(large_img) as large_img, group_num
from (
select
obj_id, case when type = 'S' then path else '' end as small_img, case when type = 'L' then path else '' end as large_img, group_num
from t_images
) a group by a.group_num;
-------- result ---------
obj_id samll_img large_img group_num
8207 /images/as.jpg /images/al.jpg 0
8207 /images/bs.jpg /images/bl.jpg 1
8207 /images/cs.jpg /images/cl.jpg 2
select max(obj_id) as obj_id, max(small_img) as small_img, max(large_img) as large_img, group_num
from (
select
obj_id, case when type = 'S' then path else '' end as small_img, case when type = 'L' then path else '' end as large_img, group_num
from t_images
) a group by a.group_num;
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。