本文共 3461 字,大约阅读时间需要 11 分钟。
以上是优化的结论:用一次请求解决n次请求查询
题目:在teacher 表中找到该 teacher下面的n个student并找出n个学生每个学生有多少个goods。
sql:
CREATE TABLE teacher ( id number NOT NULL , name varchar(100) DEFAULT NULL, PRIMARY KEY (id)) ;CREATE TABLE student ( sid number NOT NULL , sname varchar(100) DEFAULT NULL, t_s_id number NOT NULL , PRIMARY KEY (sid) ) ; insert into teacher(id,name) values(111,'zhangsan'); insert into teacher(id,name) values(222,'lisi'); insert into student(sid,sname,t_s_id) values(1,'xs1',111); insert into student(sid,sname,t_s_id) values(2,'xs2',111); insert into student(sid,sname,t_s_id) values(3,'xs3',222); insert into student(sid,sname,t_s_id) values(4,'xs4',111); select * from student;select * from teacher;select t.id,t.name,s.t_s_id,s.sid,s.sname from teacher t left join student s on t.id = s.t_s_id where t.id = 111 create table goodItem( gid number not null, goodName varchar(10), price float, good_sid number ) insert into goodItem(gid,Goodname,Price,Good_Sid) values(1,'iphone6','6000',2); insert into goodItem(gid,Goodname,Price,Good_Sid) values(2,'iphone5','5000',2); insert into goodItem(gid,Goodname,Price,Good_Sid) values(3,'iphone4','4000',2); insert into goodItem(gid,Goodname,Price,Good_Sid) values(4,'iphone3','3000',1);
vo:
package com.cn.vo;public class Student { private int sid; private String sname; public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } }
package com.cn.vo;import java.util.List;public class Teacher { private int id; private String name; private Liststudents; private List goods; public List getGoods() { return goods; } public void setGoods(List goods) { this.goods = goods; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List getStudents() { return students; } public void setStudents(List students) { this.students = students; }}
package com.cn.vo;public class Item { private Integer gid; private String goodName; private float price; private Integer good_sid; public Integer getGid() { return gid; } public void setGid(Integer gid) { this.gid = gid; } public String getGoodName() { return goodName; } public void setGoodName(String goodName) { this.goodName = goodName; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } public Integer getGood_sid() { return good_sid; } public void setGood_sid(Integer good_sid) { this.good_sid = good_sid; } }
package com.cn.vo;import java.util.List;public class GoodItem extends Item{/* private Liststudents; private List teachers; public List getStudents() { return students; } public void setStudents(List students) { this.students = students; } public List getTeachers() { return teachers; } public void setTeachers(List teachers) { this.teachers = teachers; } */ }
转载地址:http://sulfa.baihongyu.com/