이전 장의 내용에 대해 어떻게 생각하시나요?
다음으로 계속해서 조금씩 더 자세히 살펴보겠습니다.
1. 시험 점수가 60점 이상인 모든 정보 조회:
mysql> select * from(select student.s,sname,cadd,sage,sc.class,teacher.t,cname,score,tname,tadd,ssex,cphone from student,teacher,sc,course, cadd,tadd,cphone where ((((student.s=sc.s and teacher.t=course.t) and course.class=sc.class)and cadd.s=student.s)and tadd.t=teacher.t) and cphone.s=student.s)as a1 where score>60; +------+--------+--------+------+-------+------+--------+-------+--------+--------+------+--------+ | s | sname | cadd | sage | class | t | cname | score | tname | tadd | ssex | cphone | +------+--------+--------+------+-------+------+--------+-------+--------+--------+------+--------+ | 1 | 刘一 | 福建 | 18 | 2 | 2 | 数学 | 78 | 贺高 | 深圳 | 男 | 12345 | | 1 | 刘一 | 福建 | 18 | 3 | 3 | 英语 | 67 | 杨艳 | 上海 | 男 | 12345 | | 2 | 钱二 | 深圳 | 19 | 1 | 1 | 语文 | 79 | 叶平 | 福建 | 女 | 12346 | | 2 | 钱二 | 深圳 | 19 | 2 | 2 | 数学 | 81 | 贺高 | 深圳 | 女 | 12346 | | 2 | 钱二 | 深圳 | 19 | 3 | 3 | 英语 | 92 | 杨艳 | 上海 | 女 | 12346 | | 2 | 钱二 | 深圳 | 19 | 4 | 4 | 物理 | 68 | 周磊 | 湖南 | 女 | 12346 | | 3 | 张三 | 上海 | 17 | 1 | 1 | 语文 | 91 | 叶平 | 福建 | 男 | 12347 | | 3 | 张三 | 上海 | 17 | 3 | 3 | 英语 | 88 | 杨艳 | 上海 | 男 | 12347 | | 4 | 李四 | 湖南 | 18 | 2 | 2 | 数学 | 88 | 贺高 | 深圳 | 女 | 12348 | | 4 | 李四 | 湖南 | 18 | 3 | 3 | 英语 | 90 | 杨艳 | 上海 | 女 | 12348 | | 4 | 李四 | 湖南 | 18 | 4 | 4 | 物理 | 93 | 周磊 | 湖南 | 女 | 12348 | | 5 | 王五 | 江西 | 17 | 3 | 3 | 英语 | 78 | 杨艳 | 上海 | 男 | 12349 | | 6 | 赵六 | 广西 | 19 | 2 | 2 | 数学 | 68 | 贺高 | 深圳 | 女 | 13349 | | 6 | 赵六 | 广西 | 19 | 4 | 4 | 物理 | 71 | 周磊 | 湖南 | 女 | 13349 | +------+--------+--------+------+-------+------+--------+-------+--------+--------+------+--------+ 14 rows in set (0.00 sec)
2. 시험 점수가 60점 이상인 18세 남학생의 전체 정보 조회:
mysql> select * from(select student.s,sname,cadd,sage,sc.class,teacher.t,cname,score,tname,tadd,ssex,cphone from student,teacher,sc,course, cadd,tadd,cphone where ((((student.s=sc.s and teacher.t=course.t) and course.class=sc.class)and cadd.s=student.s)and tadd.t=teacher.t) and cphone.s=student.s)as a1 where (score>60 and sage=18)and ssex="男"; +------+--------+--------+------+-------+------+--------+-------+--------+--------+------+--------+ | s | sname | cadd | sage | class | t | cname | score | tname | tadd | ssex | cphone | +------+--------+--------+------+-------+------+--------+-------+--------+--------+------+--------+ | 1 | 刘一 | 福建 | 18 | 2 | 2 | 数学 | 78 | 贺高 | 深圳 | 男 | 12345 | | 1 | 刘一 | 福建 | 18 | 3 | 3 | 英语 | 67 | 杨艳 | 上海 | 男 | 12345 | +------+--------+--------+------+-------+------+--------+-------+--------+--------+------+--------+ 2 rows in set (0.00 sec)
3. 각 학생이 수강한 과목, 총점, 평균 점수 및 모든 정보(CNAME 필드 제외):
매우 단순해 보이지만 다음과 같습니다.
mysql> select count(t),sum(score),avg(score),student.s,sname,cadd,sage,sc.class,score,teacher.t,tname,tadd,ssex,cphone from(select student.s,sname,cadd,sage,sc.class,score,teacher.t,tname,tadd,ssex,cphone from student,teacher,sc,course, cadd,tadd,cphone where ((((student.s=sc.s and teacher.t=course.t) and course.class=sc.class)and cadd.s=student.s)and tadd.t=teacher.t) and cphone.s=student.s)as a1; ERROR 1054 (42S22): Unknown column 'student.s' in 'field list'
AS에 새 테이블이 있으므로 쿼리된 필드를 아래에 두지 마세요. 오래된 테이블.
mysql> select count(t),sum(score),avg(score),s,sname,cadd,sage,class,score,t,tname,tadd,ssex,cphone from(select student.s,sname,cadd,sage,sc.class,score,teacher.t,tname,tadd,ssex,cphone from student,teacher,sc,course, cadd,tadd,cphone where ((((student.s=sc.s and teacher.t=course.t) and course.class=sc.class)and cadd.s=student.s)and tadd.t=teacher.t) and cphone.s=student.s)as a1; +----------+------------+------------+------+--------+--------+------+-------+-------+------+--------+--------+------+--------+ | count(t) | sum(score) | avg(score) | s | sname | cadd | sage | class | score | t | tname | tadd | ssex | cphone | +----------+------------+------------+------+--------+--------+------+-------+-------+------+--------+--------+------+--------+ | 21 | 1483 | 70.6190 | 1 | 刘一 | 福建 | 18 | 1 | 56 | 1 | 叶平 | 福建 | 男 | 12345 | +----------+------------+------------+------+--------+--------+------+-------+-------+------+--------+--------+------+--------+ 1 row in set (0.08 sec)
그룹을 지정하지 않으면 첫 번째 행의 데이터가 바로 복사됩니다.
정답:
mysql> select a1.*,sum(score),count(t),avg(score) from(select student.s,sname,cadd,sage,sc.class,teacher.t,cname,score,tname,tadd,ssex,cphone from student,teacher,sc,course, cadd,tadd,cphone where ((((student.s=sc.s and teacher.t=course.t) and course.class=sc.class)and cadd.s=student.s)and tadd.t=teacher.t) and cphone.s=student.s)as a1 group by s; +------+--------+--------+------+-------+------+--------+-------+--------+--------+------+--------+------------+----------+------------+ | s | sname | cadd | sage | class | t | cname | score | tname | tadd | ssex | cphone | sum(score) | count(t) | avg(score) | +------+--------+--------+------+-------+------+--------+-------+--------+--------+------+--------+------------+----------+------------+ | 1 | 刘一 | 福建 | 18 | 1 | 1 | 语文 | 56 | 叶平 | 福建 | 男 | 12345 | 259 | 4 | 64.7500 | | 2 | 钱二 | 深圳 | 19 | 1 | 1 | 语文 | 79 | 叶平 | 福建 | 女 | 12346 | 320 | 4 | 80.0000 | | 3 | 张三 | 上海 | 17 | 1 | 1 | 语文 | 91 | 叶平 | 福建 | 男 | 12347 | 282 | 4 | 70.5000 | | 4 | 李四 | 湖南 | 18 | 2 | 2 | 数学 | 88 | 贺高 | 深圳 | 女 | 12348 | 271 | 3 | 90.3333 | | 5 | 王五 | 江西 | 17 | 1 | 1 | 语文 | 46 | 叶平 | 福建 | 男 | 12349 | 177 | 3 | 59.0000 | | 6 | 赵六 | 广西 | 19 | 1 | 1 | 语文 | 35 | 叶平 | 福建 | 女 | 13349 | 174 | 3 | 58.0000 | +------+--------+--------+------+-------+------+--------+-------+--------+--------+------+--------+------------+----------+------------+ 6 rows in set (0.00 sec)
4. 각 학생이 수강한 총 과목 수, 총점, 평균 점수 및 모든 정보를 조회합니다. 총점에 따라 내림차순으로 표시 평가 점수가 80점 이상인 학생의 모든 정보:
mysql> select * from(select count(t),sum(score),avg(score),s,sname,cadd,sage,class,score,t,tname,tadd,ssex,cphone from(select student.s,sname,cadd,sage,sc.class,score,teacher.t,tname,tadd,ssex,cphone from student,teacher,sc,course, cadd,tadd,cphone where ((((student.s=sc.s and teacher.t=course.t) and course.class=sc.class)and cadd.s=student.s)and tadd.t=teacher.t) and cphone.s=student.s)as a1 group by s order by sum(score) desc)a3 having avg(score)>=80; Empty set (0.00 sec)
SQL 문이 정확합니다. (CNAME 필드가 누락되었습니다.) ), 그런데 결과는...
+----------+------------+------------+------+--------+--------+------+-------+-------+------+--------+--------+------+--------+ | count(t) | sum(score) | avg(score) | s | sname | cadd | sage | class | score | t | tname | tadd | ssex | cphone | +----------+------------+------------+------+--------+--------+------+-------+-------+------+--------+--------+------+--------+ | 4 | 320 | 80.0000 | 2 | 钱二 | 深圳 | 19 | 1 | 79 | 1 | 叶平 | 福建 | 女 | 12346 | | 4 | 282 | 70.5000 | 3 | 张三 | 上海 | 17 | 1 | 91 | 1 | 叶平 | 福建 | 男 | 12347 | | 3 | 271 | 90.3333 | 4 | 李四 | 湖南 | 18 | 2 | 88 | 2 | 贺高 | 深圳 | 女 | 12348 | | 4 | 259 | 64.7500 | 1 | 刘一 | 福建 | 18 | 1 | 56 | 1 | 叶平 | 福建 | 男 | 12345 | | 3 | 177 | 59.0000 | 5 | 王五 | 江西 | 17 | 1 | 46 | 1 | 叶平 | 福建 | 男 | 12349 | | 3 | 174 | 58.0000 | 6 | 赵六 | 广西 | 19 | 1 | 35 | 1 | 叶平 | 福建 | 女 | 13349 | +----------+------------+------------+------+--------+--------+------+-------+-------+------+--------+--------+------+--------+ 6 rows in set (0.00 sec)
mysql> select * from(select count(t),sum(score),avg(score),s,sname,cadd,sage,class,score,t,tname,tadd,ssex,cphone from(select student.s,sname,cadd,sage,sc.class,score,teacher.t,tname,tadd,ssex,cphone from student,teacher,sc,course, cadd,tadd,cphone where ((((student.s=sc.s and teacher.t=course.t) and course.class=sc.class)and cadd.s=student.s)and tadd.t=teacher.t) and cphone.s=student.s)as a1 group by s order by sum(score) desc)a3 having avg(score)>0; +----------+------------+------------+------+--------+--------+------+-------+-------+------+--------+--------+------+--------+ | count(t) | sum(score) | avg(score) | s | sname | cadd | sage | class | score | t | tname | tadd | ssex | cphone | +----------+------------+------------+------+--------+--------+------+-------+-------+------+--------+--------+------+--------+ | 4 | 320 | 80.0000 | 2 | 钱二 | 深圳 | 19 | 1 | 79 | 1 | 叶平 | 福建 | 女 | 12346 | +----------+------------+------------+------+--------+--------+------+-------+-------+------+--------+--------+------+--------+ 1 row in set (0.00 sec)
그리고 이 진술과 이 결과의 모습은 나를 완전히 혼란스럽게 했습니다.
드디어 불로 맞서 싸우는 방법을 생각해서 드디어 포착했습니다. ㅋㅋㅋ
과학적 엄밀함을 바탕으로(글쎄요, 실수를 너무 많이 저질렀기 때문에 죄송합니다) 함께 확인해 보겠습니다.
mysql> select * from(select count(t),sum(score),avg(score),s,sname,cadd,sage,class,score,t,tname,tadd,ssex,cphone from(select student.s,sname,cadd,sage,sc.class,score,teacher.t,tname,tadd,ssex,cphone from student,teacher,sc,course, cadd,tadd,cphone where ((((student.s=sc.s and teacher.t=course.t) and course.class=sc.class)and cadd.s=student.s)and tadd.t=teacher.t) and cphone.s=student.s)as a1 group by s order by sum(score) desc)a3 having sum(score)/4>=80; +----------+------------+------------+------+--------+--------+------+-------+-------+------+--------+--------+------+--------+ | count(t) | sum(score) | avg(score) | s | sname | cadd | sage | class | score | t | tname | tadd | ssex | cphone | +----------+------------+------------+------+--------+--------+------+-------+-------+------+--------+--------+------+--------+ | 4 | 320 | 80.0000 | 2 | 钱二 | 深圳 | 19 | 1 | 79 | 1 | 叶平 | 福建 | 女 | 12346 | +----------+------------+------------+------+--------+--------+------+-------+-------+------+--------+--------+------+--------+ 1 row in set (0.02 sec)
mysql> select * from (select a1.*,sum(score) as ss,count(t) as ct,avg(score) as a3 from(select student.s,sname,cadd,sage,sc.class,teacher.t,cname,score,tname,tadd,ssex,cphone from student,teacher,sc,course, cadd,tadd,cphone where ((((student.s=sc.s and teacher.t=course.t) and course.class=sc.class)and cadd.s=student.s)and tadd.t=teacher.t) and cphone.s=student.s)as a1 group by s desc)as a2 where a3=80 or a3>80; +------+--------+--------+------+-------+------+--------+-------+--------+--------+------+--------+------+----+---------+ | s | sname | cadd | sage | class | t | cname | score | tname | tadd | ssex | cphone | ss | ct | a3 | +------+--------+--------+------+-------+------+--------+-------+--------+--------+------+--------+------+----+---------+ | 4 | 李四 | 湖南 | 18 | 2 | 2 | 数学 | 88 | 贺高 | 深圳 | 女 | 12348 | 271 | 3 | 90.3333 | | 2 | 钱二 | 深圳 | 19 | 1 | 1 | 语文 | 79 | 叶平 | 福建 | 女 | 12346 | 320 | 4 | 80.0000 | +------+--------+--------+------+-------+------+--------+-------+--------+--------+------+--------+------+----+---------+ 2 rows in set (0.00 sec)
위 내용은 mysql에서 7테이블 쿼리를 구현한 예 (2)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!