4.MySQL表的增删改查(进阶)
增加记录:
INSERT INTO students (id, name, age) VALUES (1, '张三', 20);
删除记录:
DELETE FROM students WHERE id = 1;
更新记录:
UPDATE students SET age = 21 WHERE id = 1;
查询记录:
SELECT * FROM students WHERE age > 18;
批量更新:
UPDATE students SET age = age + 1 WHERE id IN (1, 2, 3);
复合查询(AND, OR, NOT, LIKE):
SELECT * FROM students WHERE age > 18 AND gender = 'male';
SELECT * FROM students WHERE age < 20 OR age > 30;
SELECT * FROM students WHERE NOT name LIKE '张%';
排序和限制结果数量:
SELECT * FROM students ORDER BY age DESC LIMIT 5;
分组和聚合:
SELECT gender, COUNT(*) FROM students GROUP BY gender;
连接查询(JOIN):
SELECT students.name, courses.name FROM students JOIN courses ON students.id = courses.student_id;
子查询:
SELECT name FROM students WHERE id IN (SELECT student_id FROM scores WHERE score > 80);
这些是MySQL表的增删改查操作的进阶用法,涵盖了常用的SQL操作。
评论已关闭