我使用postgres作为数据库
我想按单位显示员工
我尝试使用此查询,但结果员工未按单位分组
select employee_lang.fullname_emp_lang,unit_lang.name_unit_lang
from employee employee,employee_lang employee_lang,unit unit,unit_lang unit_lang
where employee.id_employe= employee_lang.id_employe
and employee_lang.lang_emp_lang='ar'
and employee.id_unit= unit.id_unit
and unit.id_unit= unit_lang.id_unit
and unit_lang.lang_unit_lang='ar'
group by employee_lang.fullname_emp_lang,unit_lang.name_unit_lang
例如我想要这种结果
alen dep1
franc dep1
med dep1
juk dep2
sizar dep2
我也尝试使用
group by unit_lang.name_unit_lang
意思
select employee_lang.fullname_emp_lang,unit_lang.name_unit_lang
from employee employee,employee_lang employee_lang,unit unit,unit_lang unit_lang
where employee.id_employe= employee_lang.id_employe
and employee_lang.lang_emp_lang='ar'
and employee.id_unit= unit.id_unit
and unit.id_unit= unit_lang.id_unit
and unit_lang.lang_unit_lang='ar'
group by unit_lang.name_unit_lang
我有这个错误:
ERROR: column "employee_lang.fullname_emp_lang" must appear in the GROUP BY clause or be used in an aggregate function
答案 0 :(得分:0)
我认为你是在接受订单,而不是分组:
select employee_lang.fullname_emp_lang,unit_lang.name_unit_lang
from employee employee,employee_lang employee_lang,unit unit,unit_lang unit_lang
where employee.id_employe= employee_lang.id_employe
and employee_lang.lang_emp_lang='ar'
and employee.id_unit= unit.id_unit
and unit.id_unit= unit_lang.id_unit
and unit_lang.lang_unit_lang='ar'
order by unit_lang.name_unit_lang