如何查询合并一条记录。

frank630 2013-06-03 10:53:50
我有表 Point
------------------------
id | point1 | point2|
------------------------
1 | 2.23 | null |
------------------------
1 | null | 4.34 |
------------------------
2 | null | 3.34 |
------------------------
2 | 4.34 | null |
------------------------

现在我想查询合并id相同的记录,已知表中有每两条记录的id相同,其中一条记录的point1为空,另一条记录的point2为空,我想的到的结果是这样 1,2.23,4.34 和 2,4.34,3.34 应该怎么样写sql语句,求解答。谢谢。
...全文
428 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
知道就是你 2013-07-26
  • 打赏
  • 举报
回复
上边表结构如下: CREATE TABLE IF NOT EXISTS ` point` ( `id` int(11) NOT NULL AUTO_INCREMENT, `sid` int(11) NOT NULL, `point1` float DEFAULT NULL, `point2` float DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ; -- -- 转存表中的数据 ` point` -- INSERT INTO ` point` (`id`, `sid`, `point1`, `point2`) VALUES (1, 2, 2.34, NULL), (2, 2, NULL, 4.34), (3, 1, 3.34, NULL), (4, 1, NULL, 4.34);
知道就是你 2013-07-26
  • 打赏
  • 举报
回复
一条SQL搞定 : SELECT concat(`sid`,',',GROUP_CONCAT(point1) ,',', GROUP_CONCAT(point2) ) FROM ` point` WHERE 1 group by sid ; concat(`sid`,',',GROUP_CONCAT(point1) ,',', GROUP_CONCAT(point2) ) 1,3.34,4.34 2,2.34,4.34
tashiwoweiyi 2013-06-04
  • 打赏
  • 举报
回复
我想说的是,你只要你上面的这种结果语句吗? 如果相同ID的point1 或point2的值都不为空,是加起来,还是?取哪个?
ACMAIN_CHM 2013-06-04
  • 打赏
  • 举报
回复
每两条记录的id相同,其中一条记录的point1为空,另一条记录的point2为空 select a.id,a.point1,b.point2 from Point a,Point b where a.id=b.id and a.point1 is not null and b.point2 is not null
WWWWA 2013-06-04
  • 打赏
  • 举报
回复
select `id`, sum(COALESCE(point1,0)) as point1, sum(COALESCE(point2,0)) as point2 from point group by `id`
齐岳 2013-06-04
  • 打赏
  • 举报
回复
select id,
sum(ifnull(point1,0)) as point1,
sum(ifnull(point2,0)) as point2

from Point

group by id
yo_yo1120 2013-06-04
  • 打赏
  • 举报
回复
select id,
       sum(ifnull(point1,0)) as point1,
       sum(ifnull(point2,0)) as point2
 
from point

group by id
rucypli 2013-06-04
  • 打赏
  • 举报
回复
引用 1 楼 u010581018 的回复:
select id,
       sum(ifnull(point1,0)) as point1,
       sum(ifnull(point2,0)) as point2
 
from point

group by id

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧