首先需要建立两张有关联的表,随便建了个课表和学生信息。
设置一个外键使两张表形成关联
下面是简单SQL语句的运用
查询
SELECT id,studentname FROM students;
筛选出不重复的课程
SELECT DISTINCT classID FROM students;
按某一顺序排列
SELECT studentname,age,studentID
FROM students
ORDER BY studentID;
在表中插入数据
INSERT INTO curriculum
VALUES (1006,"体育","彭老师");
搜索列中的指定模式
SELECT studentID
FROM students
WHERE studentID LIKE '%4';
搜索两表中相匹配的列
SELECT classID
FROM students
INNER JOIN curriculum
ON students.classID=curriculum.id
返回左表中所有的行 即使右表中没有匹配 没有匹配即显示为Null
SELECT studentname,classID
FROM students
LEFT JOIN curriculum
ON students.classID=curriculum.id
删除表中某数据
DELETE FROM students
WHERE id=7;
修改表中数据
UPDATE curriculum
SET classname='思修',teacher='周老师'
WHERE id='1002';
网友评论