简体   繁体   English

一对多关系显示为ajax结果

[英]One-to-Many Relationship displayed as ajax result

I have two table department and city 我有两个表departmentcity

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

and I make an Ajax call to display all departments and their cities. 我打电话给Ajax,以显示所有部门及其所在的城市。

I run this query : 我运行此查询:

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

The problem is that I got one department repeated in many rows because I have one to many relationship . 问题是我因为有一对多的关系而使一个部门重复了很多行。

The question is what is the best, cleanest and easiest way to display the query result with Ajax in Javascript / jQuery with $.each() loop? 问题是用$.each()循环在Javascript / jQuery中使用Ajax显示查询结果的最佳,最$.each()和最简便的方法是什么?

How to handle them and display them in my html page? 如何处理它们并在我的html页面中显示它们?

I want display them as checkbox tree in a popup and the user check what he want to do a search operation. 我想在弹出窗口中将它们显示为复选框树,然后用户检查他要执行的搜索操作。

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

unless you need to explicit the joint you can try this query. 除非您需要明确联合,否则可以尝试此查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM