================= SQL 四大语句 ===========================
1.删: DLEATE
DELETE FROM 表 WHERE 条件
2.增 INSERT
INSERT INTO 表(字段列表) VALUES(值列表)
3.改 UPDATE
UPDATA 表(字段列表) SET (字段=值,字段=值,。。。。) WHERE 条件
4.查 SELECT
SELECT * FROM 表
子句:
=================== WHERE 条件 ==========================
WHERE name = ‘blue’
WHERE age = 18
WHERE age<18
WHERE age>= 18
WHERE age>=18 AND score<60
WHERE cach>100 OR score>10000
===================== ORDER 排序 ==========================
ORDER BY age ASC/DESC
ASC-升序(从小到大)
DESC-降序
ORDER BY price DESC
ORDER BY price ASC
*价格(price)升序排列,如果价格相同再按照销(asles)量排序
ORDER BY price ASC, sales DESC
================= GROUP 聚类–合并相同 =====================
*统计每个班人数
COUNT —————– 计数
SELECT COUNT(*) FORM class_table ==> 统计class表内总数
SELECT * FROM class_table GROUP BY class; ==>去重
SELECT class,COUNT(class) FROM class_table GROUP BY class; ==> 查询class表,计数class 从class_table
GROUP 统计
*统计每个班平均分数
SELECT * FORM student_table; ==>
SELECT * FROM student_table GROUP BY CLASS; ==>
SELECT class, AVG(score) FROM student_table GROUP BY class; ==>
*每个班级的最高、最低分
SELECT class,MAX(score),MIN(score) FROM student_table GROUP BY class;
*价格总价
SELECT name,price FROM sales_table GROUP BY name;
SELECT name,SUM(price) FROM sales_table GROUP BY name ORDER BY SUM(price) DESC;
=============== LIMIT –限制输出 分页 ===============
1.所有数据给前段
2.后台只给部分数据
LIMIT 10 从10页开始
LIMIT 5,8 从5页开始到后8页
第1页 0,20 0~19
第2页 20,20 20~39
第n页 (n-1),20 n~n+20
字句之间顺序
WHERE ==> GROUP ==> ORDER ==> LIMIT
SELECT class,COUNT(class) FROM student_table
WHERE score>60
GROUP BY class
ORDER BY COUNT(class) DESC
LIMIT 2;
数据库导出 — 右键 — 转储SQL文件
数据库导入 — 右键 — 运行SQL文件