数据库SQL优化

Postgres数据库organization表:

CREATE TABLE ORGANIZATION( -- 分公司与部门表
	DWBH INTEGER NOT NULL, -- 部门ID
	DWMC VARCHAR(50) NOT NULL, --部门名称
	FGS INTEGER NOT NULL, --分公司ID
	FGSMC VARCHAR(50) NOT NULL --分公司名称
)

优化前:
select concat(fgsmc,'---',dwmc) as dwmc from organization where fgsmc!=dwmc 
union all select distinct dwmc from organization where fgsmc=dwmc

优化后:
select case when fgsmc!=dwmc then concat(fgsmc,'---',dwmc) 
when fgsmc=dwmc then dwmc end as dwmc from organization























猜你喜欢

转载自blog.csdn.net/u010520912/article/details/49054537