问题标题:
现有表t_task_vs_ip结构如下:task_idstart_ipend_ip内容如下:10.0.0.00.0.0.12192.168.0.1192.168.0.12192.168.0.13192.168.0.132192.168.0.16192.168.0.16要求经过查询以后变为:
问题描述:
现有表t_task_vs_ip结构如下:task_idstart_ipend_ip内容如下:
10.0.0.00.0.0.1
2192.168.0.1192.168.0.1
2192.168.0.13192.168.0.13
2192.168.0.16192.168.0.16
要求经过查询以后变为:
task_idstart_ip end_ip
1 0.0.0.0 0.0.0.1
2 192.168.0.1 192.168.0.16
这样的查询结果该如何实现呢?
曹锦章回答:
CREATETABLEt_task_vs_ip(task_idint,start_ipvarchar(15),end_ipvarchar(15));GOINSERTINTOt_task_vs_ipSELECT1,'0.0.0.0','0.0.0.1'UNIONALLSELECT2,'192.168.0.1','192.168.0.1'UNIONALLSELECT2,'...
李成俊回答:
恩,对我也是用的这个方法,如果现在以ID分组,列出所有IP又该如何实现呢?
曹锦章回答:
SELECTtask_id,STUFF((SELECT','+ipFROM(SELECTstart_ipIPFROMt_task_vs_ipsubTitleWHEREtask_ id=t_task_vs_ip.task_idUNIONSELECTend_ipIPFROMt_task_vs_ipsubTitleWHEREtask_ id=t_task_vs_ip.task_id)subQueryFORXMLPATH('')),1,1,'')ASallTitleFROMt_task_vs_ipGROUPBYtask_idtask_idallTitle---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------10.0.0.0,0.0.0.12192.168.0.1,192.168.0.13,192.168.0.16(2行受影响)这个效果?
点击显示
其它推荐