-- 查询参加了考试的同学(学号,姓名,科目编号,分数)
/*思路
1.分析需求,分析查询的字段来自于哪些表(连接查询)
2.确定使用哪种连接查询?
确定交叉点(这两个表中的哪些数据是相同的)
判断的条件:学生表中的`studentno`=成绩表中的`studentno`
*/
SELECT s.`studentno`,s.`studentname`,r.`subjectno`,r.`studentresult`
FROM `student` AS s
INNER JOIN `result` AS r
ON s.`studentno`=r.`studentno`
-- right join
SELECT s.`studentno`,`studentname`,`subjectno`,`studentresult`
FROM `student` AS s
RIGHT JOIN `result` AS r
ON s.studentno=r.studentno
-- letf join
SELECT s.`studentno`,`studentname`,`subjectno`,`studentresult`
FROM `student` AS s
LEFT JOIN `result` AS r
ON s.studentno=r.studentno
-- 查询缺考的同学
SELECT s.`studentno`,`studentname`,`subjectno`,`studentresult`
FROM `student` AS s
LEFT JOIN `result` AS r
ON s.studentno=r.studentno
WHERE `studentresult` IS NULL
-- 思考题(查询了参加考试的同学信息:学号,学生姓名,科目编号,科目名,分数)
/*思路:
1.分析需求,分析查询的字段来自哪些表,student、result、subject(连接查询)
2.确定使用哪种连接查询?
确定交叉点(这两个表中哪个数据是相同的)
判断的条件:学生表的中studentNo =成绩表studentNo
*/
SELECT s.`studentno` AS "学号",`studentname` AS "姓名",r.`subjectno` AS "科目编号",`subjectname` AS "科目名",`studentresult` AS "分数"
FROM `student` AS s
RIGHT JOIN `result` AS r
ON s.`studentno`=r.`studentno`
INNER JOIN `subject` AS sub
ON r.`subjectno`=sub.`subjectno`
-- 查询参加 数据库结构-1 考试的同学信息:学号,学生姓名,科目名,分数
SELECT s.`studentno`,`studentname`,`subjectname`,`studentresult`
FROM `student` AS s
INNER JOIN `result` r
ON s.`studentno`=r.`studentno`
INNER JOIN `subject` sub
ON r.`subjectno`=sub.`subjectno`
WHERE `subjectname`="数据库结构-1"
总结:
先执行下列语句:
CREATE TABLE `category`(
`categoryid` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT "主题id",
`pid` INT(10) NOT NULL COMMENT "父id",
`categoryname` VARCHAR(50) NOT NULL COMMENT "主题名字",
PRIMARY KEY(`categoryid`)
)ENGINE=INNODB AUTO_INCREMENT=9 CHARSET=utf8
INSERT INTO `category`(`categoryid`,`pid`,`categoryname`)
VALUES("2","1","信息技术"),
("3","1","软件开发"),
("4","3","数据库"),
("5","1","美术设计"),
("6","3","web开发"),
("7","5","ps技术"),
("8","2","办公信息")
自己的表和自己的表连接,核心:一张表拆成2张一样的表
-- 查询父子信息
SELECT a.`categoryname` AS "父栏目",b.`categoryname` AS "子栏目"
FROM `category` AS a,`category` AS b
WHERE a.`categoryid`=b.`pid`