页面载入中...

当前位置: 感悟生活网 > 吕布简介正文

SQL心得学习

SQL经验学习
写一句SQL语句找出没有Student的Class

select class.name from class left join student on  class.id=student.c_id where  student.name is null;


对于Class里面的totalScore,计算规则是此Class下所有学生score的总和,写一句SQL实现全表totalScore的更新

update class set TOTAL_SCONE=(select sum(SCORE) from student where class.id=student.c_id)


如果class里有错误的totalScore记录(不瞒住上面计算逻辑的),写一句SQL查找出所有totalScore记录不正确的Class。

select * from class where class.total_scone <> (select sum(SCORE) from student where class.id=student.c_id)


在之前的某个方法中输出了一个学生集合Collection<Student>,现希望在代码中对其学生名称(name)进行排序(补充:学生没有重名的)
class TestSort:
public class TestSort{
	
	public static void main(String args[]){
		TestObj obj1=new TestObj("chensheng1","n");
		TestObj obj2=new TestObj("chensheng2","n");
		TestObj obj3=new TestObj("chensheng3","s");
		TestObj obj4=new TestObj("chensheng4","n");
		TestObj obj5=new TestObj("chensheng5","s");
		TestObj obj6=new TestObj("chensheng6","s");
		Collection<TestObj> list=new ArrayList<TestObj>();
		list.add(obj1);
		list.add(obj3);
		list.add(obj4);
		list.add(obj2);
		list.add(obj6);
		list.add(obj5);

		
		Collections.sort((ArrayList)list);
		
		for(TestObj k : list){
			System.out.println(k.getName() +"--"+k.getSex());
		}
	}
}

class TestObj:
public class TestObj implements Comparable {

	private String name;
	private String sex;
	
	public TestObj(String name,String sex){
		this.name=name;
		this.sex=sex;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name=name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex=sex;
	}

	@Override
	public String toString() {
		return "name: "+getName() +" sex: "+getSex();
	}

	@Override
	public int compareTo(Object o) {
		return this.name.compareTo(((TestObj)o).name);
	}
}

《SQL心得学习》:等您翻牌子呢!

发表评论