一对多关系显示为ajax结果

时间:2015-05-20 09:12:27

标签: javascript sql ajax

我有两张表departmentcity

city ( #id (PK) , name , id_department (FK) , ... ) 
department( #id (PK) , name , ... )

我打了一个Ajax调用来显示所有部门及其城市。

我运行此查询:

select distinct d.id , d.nom , c.id , c.nom 
from department d 
left join city c on d.id = c.id_department 

问题在于我在一行中重复了一个部门,因为我有一对多的关系。

问题是使用$.each()循环在Javascript / jQuery中使用Ajax显示查询结果的最佳,最干净和最简单的方法是什么?

如何处理它们并在我的html页面中显示它们?

我想在弹出窗口中将它们显示为复选框树,用户检查他想要执行搜索操作。

1 个答案:

答案 0 :(得分:0)

select d.id, d.nom, c.id, c.nom from department d, city c
where d.id = c.id_departement

除非您需要明确联合,否则您可以尝试此查询。