我有两张桌子:
Institute:
-----------
id name city
----- ------- -------
1 Name 1 London
2 Name 2 Leeds
3 Name 3 Edinburgh
Course:
--------
id i_id name stream
----- ----- -------------------- -------
1 1 Computer Engineering Engineering
2 1 Chemical Engineering Engineering
3 2 Electronics Engineering Engineering
4 3 Mechanical Engineering Engineering
我的结果集中需要以下信息:
- 提供工程课程的机构(名称 1、名称 2、名称 3)。
- 学院提供的所有课程。
现在,这是我实现此结果集的查询:
SELECT institute.id, institute.name AS i_name, institute.city, course.name AS c_name
FROM institute, course
WHERE institute.id = course.i_id
AND course.stream = 'Engineering'
ORDER BY institute.id
此查询返回 4 条记录:
id i_name city c_name
--- ------- --------- ----------------
1 Name 1 London Computer Engineering
1 Name 1 London Chemical Engineering
2 Name 2 Leeds Electronics Engineering
3 Name 3 Edinburgh Mechanical Engineering
如何迭代此结果集,以便获得网页上的以下数据列表:
Found 3 Institutes
----------------------
1) Name 1, London
Courses Offered:-
=> Computer Engineering
=> Chemical Engineering
2) Name 2, Leeds
Courses Offered:-
=> Electronics Engineering
3) Name 3, Edinburgh
Courses Offered:-
=> Mechanical Engineering
[添加]
我的网页上有分页设置。现在假设我想每页显示 2 条记录,我将使用LIMIT 0,2
. 现在很明显,我查询中的前 2 条记录给了我相同的机构,所以在这种情况下,我也需要第三个机构。
我可以进行这样的动态查询吗?