
1 qwa123 OP 有人吗... |
2 yuankui 2019-02-16 19:29:51 +08:00 group by + group_concat. 兄弟,听我一句劝,伸手党在 V2EX 是混不下去的,好好打一下基础,提一点对大家有价值的问题。 |
3 keyfunc 2019-02-16 20:12:18 +08:00 作业自己做系列 |
4 azh7138m 2019-02-16 20:26:29 +08:00 via Android 楼主微博名字好评 2L 正解 |
5 Cbdy 2019-02-16 20:29:14 +08:00 via Android MySQL 不要用驼峰 |
6 GuangXiN 2019-02-16 20:30:59 +08:00 via Android 用驼峰也没啥问题 |
7 Cbdy &nbs; 2019-02-16 20:42:41 +08:00 |
8 newlife 2019-02-16 23:48:58 +08:00 select "batchId", case when type::integer=1 then array_agg("bizId") else null end as "uid" , case when type::integer=2 then array_agg("bizId") else null end as "fid" from V2EX group by "batchId",type; result: "batchId","uid","fid" "1", "['uid1', 'uid2']",NULL "3", NULL, "['fid3']" "1", NULL, "['fid1', 'fid2']" "2", "['uid3']", NULL 这个和楼主的要求还不符,求解答,,,如何合并第一和第三项?我用的 postgre |
10 newlife 2019-02-17 00:31:43 +08:00 select "batchId", array_remove(array_agg(case when type::integer=1 then "bizId" end ),NULL)as "uid" , array_remove(array_agg(case when type::integer=2 then "bizId" end),NULL )as "fid" from V2EX group by "batchId"; result: "3" "{}" "{fid3}" "2" "{uid3}" "{}" "1" "{uid1,uid2}" "{fid1,fid2}" 这次对了,还有别的写法么? |
11 GuangXiN 2019-02-17 16:33:59 +08:00 @Cbdy 你贴第一个是给 MySQL 写 test cases 的时候需遵循的规范,第二个是该 blog 作者所在团队的规范。自己开发使用 MySQL 的时候,遵守自己团队规范即可,MySQL 本身可以支持驼峰命名的表名和字段名,innodb 存储引擎也不会把表名当文件名保存,不会触发大小写敏感的问题。 |
12 Cbdy 2019-02-17 16:47:29 +08:00 @GuangXiN 这不是技术上的,这是规约上的。在实践中,MySQL 甚至可以用中文作为列名但很少有人这么做 我问过很多 DBA,他们建议不要使用驼峰。出于“最小惊讶原则”,选用主流的规约是好的。所以我给出这个建议 |
13 simple2025 2019-02-17 18:38:57 +08:00 工作几年 sql 只会简单的 sql 了 |
15 pupilGradeSix 2019-02-18 00:28:51 +08:00 可能对于专业的来说太简单了,但我作为一个前端,你这个 sql 着实让我感兴趣了啊。研究了好一会 ``` SELECT batchId, group_concat( CASE WHEN type = 1 THEN bizId ELSE null END) as "uid", group_concat( CASE WHEN type = 2 THEN bizId ELSE null END) as "typeid" FROM test GROUP BY batchId; ``` |