前台: dhtmlxgrid.显示数据

    其格式为:

 {
  rows:[
     {id:1,data:[1,2,3]}
,{}
]
}

如果在postgesql里直接生成这样的串呢??

这是就今天要做的事.

也是测试了一天,还是别人的帮助下完成:

1,2  本人自己写的,怎么也达不成这个目标:

3,    群友给的方案:完美解决


--方案1:
select json_agg(row_to_json(t))::text from (select id,concat_ws(',',pt_name,pt_description) as mydata from project_template )as t;
--结果[{"id":1,"myData":"whq,admin"},{"id":2,"myData":"eathon,sys"}]
--不理想
--方案2:
select json_agg(row_to_json(t))::text from (select id,'['|| concat_ws(',',pt_name,pt_description) ||']' as mydata from project_template ) as t
--结果[{"id":1,"myData":["whq,admin"]},{"id":2,"myData":["eathon,sys"]}]
好像对了,可是仔细看:"myData":["eathon,sys"]
    不对,应该是:"myData":["eathon","sys"]
--方案3:
select json_agg(row_to_json(t))::text from (select id,array[pt_name,pt_description] as mydata from project_template where 1=1 and id=1) as t
--结果:[{"id":1,"myData":["whq","admin"]},{"id":2,"myData":["eathon","sys"]}] --但是,大家注意了哟: 里面的数值是不带引号的,如果 用 JQUERY 的 $.getJSON()接受的话.里面的JSON 串 KEY,VALUE 都必须是双引号,否则不接受!
--见另一篇文章

$.getJSON(url,function success(){})回调函数不起作用


 --那么问题来了:  
--问题1:如果想返回所有的 KEY VALUE都带双引号该怎么办?
--

select json_agg(row_to_json(t))::text from (select id::text,array[pt_name,pt_description] as mydata from project_template where 1=1  and id=1) as t
--问题2: 如果查询的字段是个'col1,col2,col3'这样的变量应该怎么办?
--

select array_to_string(string_to_array('col1,col2,col3',','),'::text,')  --结果: col1::text,col2::text,col3

--快了,快要接近了:最后还少一个  ::text,因为要把所有的字段都给字符串化

--

select array_to_string(string_to_array('col1,col2,col3',','),'::text,')||'::text'   --结果:col1::text,col2::text,col3::text

--这样才算完美解决!

--这里再次感谢  pumpkin 群友!(没有你的指点,我不知道要走多少弯路!)

--也放一个自己'研究'了N久的一个查询存储过程

--其可以完美解决 生成 标准的JSON(都带双引号)

create or replace function f_query_all_json(
table_name text, --表名
query_feilds text, --需要查询的字段和字段值 'col1,col2,col3.......'
condition_feilds text, --条件字段和字段值  "[{\"feild_name\":\"id\",\"feild_value\":1}]" 当然,这里应该可以简化一下
out return_value text --返回值
) as $$
declare
ex_sql text;
recs record;
_key text ;
_value text;
_result text;
_counts integer;
begin
--这里有个问题.就是query_feilds 里面字段如果不是TEXT 类型,得加上强制转换::text
select array_to_string(string_to_array($2,','),'::text,')||'::text' into _key;
ex_sql:='select json_agg(row_to_json(t))::text from (select id::text,array['||_key||'] as data from '
||quote_ident(table_name)
|| ' where 1=1 ';
--设置条件参数
for recs in select * from json_array_elements(condition_feilds::json) loop
_key := recs.value ->> 'feild_name';
_value := recs.value ->> 'feild_value' ; if json_typeof(recs.value -> 'feild_value') ='number' then
ex_sql:=ex_sql|| ' and ' || _key || '=' || _value;
else
ex_sql:=ex_sql|| ' and ' || _key || ' like '''|| (recs.value ->> 'feild_value') || '%''';
end if;
end loop;
ex_sql:=ex_sql||') as t';
execute ex_sql into _value;
_result:='{"rows":'|| _value||'}';
return_value := _result;
-- return_value :=ex_sql;
end;
$$ language plpgsql;
 

 

一个 很牛老外写的SQL语句

CREATE TABLE customer_area_node
(
id bigserial NOT NULL PRIMARY KEY,
customer_id integer NOT NULL,
parent_id bigint,
name text,
description text
); INSERT INTO customer_area_node(customer_id, parent_id, name, description) VALUES
(1, NULL, 'name1', ''),
(2, 1, 'name2', '1.2'),
(3, 1, 'name3', '1.3'),
(4, 2, 'name4', '1.2.4'),
(5, 2, 'name5', '1.2.5'),
(6, 3, 'name6', '1.3.6'),
(7, 3, 'name7', '1.3.7'),
(8, 5, 'name8', '1.2.5.8'),
(9, 6, 'name9', '1.3.6.9'),
(10, 3, 'name10', '1.3.10'),
(11, 9, 'name11', '1.3.6.9.11'),
(12, 11, 'name12', '1.3.6.9.11.12'); WITH RECURSIVE c AS (
SELECT *, 0 as lvl
FROM customer_area_node
WHERE customer_id = 1 AND parent_id IS NULL
UNION ALL
SELECT customer_area_node.*, c.lvl + 1
FROM customer_area_node
JOIN c ON customer_area_node.parent_id = c.id
),
maxlvl AS (
SELECT max(lvl) maxlvl FROM c
),
j AS (
SELECT c.*, json '[]' children
FROM c, maxlvl
WHERE lvl = maxlvl
UNION ALL
SELECT (c).*, array_to_json(array_agg(j) || array(SELECT r
FROM (SELECT l.*, json '[]' children
FROM c l, maxlvl
WHERE l.parent_id = (c).id
AND l.lvl < maxlvl
AND NOT EXISTS (SELECT 1
FROM c lp
WHERE lp.parent_id = l.id)) r)) children
FROM (SELECT c, j
FROM c
JOIN j ON j.parent_id = c.id) v
GROUP BY v.c
)
SELECT row_to_json(j) json_tree
FROM j
WHERE lvl = 0;

postgresql+ C#+ DHTMLX 学习汇总的更多相关文章

  1. ABP 学习汇总

    本文背景 公司最近规划的新框架准备基于ABP来搭建,自从在阳铭博客看到ABP框架的介绍后,就一直持续关注着,但还没真正在实际项目中直接使用ABP,只是自己做了一些学习和Demo.ABP所用到的一些新技 ...

  2. JS object(对象)的学习汇总

    Object(对象)是在所有的编程语言中都十分重要的一个概念,对于事物我们可以把他们看作是一个对象,而每一个事物都有自己的表示的属性和对于某一信息作出的相应的操作.而这些东西就变成了事物的属性和方法. ...

  3. MINA学习汇总

    MINA学习汇总 Apache Mina Server 是一个网络通信应用框架,用于开发高性能和高可用性的网络应用程序.它主要是对基于TCP/IP.UDP/IP协议栈的通信框架(然,也可以提供JAVA ...

  4. HTTP头学习汇总

    在开发http请求的时候,对HTTP头部信息一知半解,各种百度谷歌汇总一下学习到的资料.   http简介 HTTP(HyperTextTransferProtocol)是超文本传输协议的缩写,它用于 ...

  5. 《A Tour of PostgreSQL Internals》学习笔记——系统表和数据类型

    上周末学习了<A Tour of PostgreSQL Internals>的第一部分(View 1),今天我们继续打开书本,继续View 2 部分. View 2 Postgresql的 ...

  6. 《A Tour of PostgreSQL Internals》学习笔记——进程间通信

    中秋节假期这么快就没了,这几天还一直下雨,索性在家看看书.这次看的是Tom Lane的<A Tour of PostgreSQL Internals>.这篇小随笔就算做学习笔记了.园子里面 ...

  7. sencha touch 学习汇总(转)

    1.官方网站:http://www.sencha.com/products/touch/ 2.在线文档:http://docs.sencha.com/touch/2.2.1/ 3.在线翻译文档:htt ...

  8. 《A Tour of PostgreSQL Internals》学习笔记——查询处理分析

           终于要迎来postgresql的<A Tour of PostgreSQL Internals>系列的最后一篇了.学习是不能拖延的事儿,越拖延事情越多.不废话,一起来看看吧~ ...

  9. Eucalyptus学习汇总

    Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus) 是一种开 ...

随机推荐

  1. java项目文件的路径问题

    title: 项目下的路径问题 tags: grammar_cjkRuby: true --- 在javaee的项目中,存取文件,解析xml和properties文件,以及项目中的文件,都需要获取路径 ...

  2. zk 05之:ZooKeeper的配置

    ZooKeeper 的功能特性通过 ZooKeeper 配置文件来进行控制管理( zoo.cfg 配置文件). ZooKeeper 这样的设计其实是有它自身的原因的.通过前面对 ZooKeeper 的 ...

  3. Lagom学习 五 Hello world工程

    用Maven创建一个Hello world的Lagom工程: 1: 在想创建工程的目下下,打开CMD 2:  mvn archetype:generate -Dfilter=com.lightbend ...

  4. python set集合的用法

    python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...

  5. C#操作SQL Server中的Image类型数据

    该例子是一个对SQL Server数据类型的一个操作例子,具有写入.读取功能. 1:准备数据库 1)创建数据库 Test 2)创建表 Table_1 (分别有2个字段:id(Int).photo(Im ...

  6. jquery效果基础运用

    jQuery 版本 2 以上不支持 IE6,7,8 浏览器.如果需要支持 IE6/7/8,那么请选择1.9你还可以通过条件注释在使用 IE6/7/8 时只包含进1.9.<!--[if lt IE ...

  7. iView之清空选择框

    Form表单布局的vue组件,已经增加了校验选择框,判断为空的情况下不调用接口. 后来发现,选择了选择框后,清空,再点查询,还是会调接口,看日志发现传了原来清空的值过来,实际上没有清空. 这里增加on ...

  8. UVaLive 4094 WonderTeam (贪心)

    题意:有n支队伍,每两支队伍打两场比赛(主客场各一次),胜得3分,平得1分,输不得分,比赛结束之后会评选出一个梦之队, 梦之队满足以下条件:进球总数最多,胜利场数最多,丢求总数最少,三个都不能并列,求 ...

  9. ASP.NEt ajax 弹出窗口在页面无法关闭

    <meta http-equiv="X-UA-Compatible" content="IE=9" />     今天又有客戶投訴公司的網頁有問題. ...

  10. Ubuntu 16 Mysql 安装配置

    安装Mysql   apt-get update;   apt-get install mysql-server apt-get install mysql-client //安装过程中会提示修改密码 ...