转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11782549.html
MySQL--数据表操作:
会用到的查询操作.
1. Limit的用法:
查询从下标5开始往后的10条数据(下标默认为0开始) SELECT * FROM stu_user LIMIT 5, 10; 查询从下标5开始往后的所有数据(下标默认为0开始) SELECT * FROM stu_user LIMIT 5,- 1; 查询前5条数据 SELECT * FROM stu_user LIMIT 5;
2. 行转列和列转行
a) 列转行
创建-->学生-科目分数表 (这种建表方式需要提前把所有的科目列都设计好,如果往后需要添加科目的话需要改动表结构) CREATE TABLE stu_score ( stu_id INT NOT NULL, sname VARCHAR ( 20 ) NOT NULL, chscore CHAR ( 3 ) DEFAULT '0', mathscore CHAR ( 3 ) DEFAULT '0', enscore CHAR ( 3 ) DEFAULT '0' ); 表结构 +-----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+-------+ | stu_id | int(11) | NO | | NULL | | | sname | varchar(20) | NO | | NULL | | | chscore | char(3) | YES | | 0 | | | mathscore | char(3) | YES | | 0 | | | enscore | char(3) | YES | | 0 | | +-----------+-------------+------+-----+---------+-------+ 插入数据 INSERT INTO stu_score VALUES ( 1, '张三', '77', '88', '66' ); INSERT INTO stu_score VALUES ( 2, '李四', '23', '45', '13' ); INSERT INTO stu_score VALUES ( 3, '王五', '94', '63', '45' ); 数据列表 +----+--------+---------+-----------+---------+ | id | sname | chscore | mathscore | enscore | +----+--------+---------+-----------+---------+ | 1 | 张三 | 77 | 88 | 66 | | 2 | 李四 | 23 | 45 | 13 | | 3 | 王五 | 94 | 63 | 45 | +----+--------+---------+-----------+---------+ 列转行(科目分数列转换为固定的科目值行数据和分数汇总列) 本质就是用UNION合并分批查询的数据, 有多少科目列需要转则分批多少次查询 SELECT stu_id, sname, '语文' AS course, chscore AS score FROM stu_score UNION SELECT stu_id, sname, '数学' AS course, mathscore AS score FROM stu_score UNION SELECT stu_id, sname, '英语' AS course, enscore AS score FROM stu_score ORDER BY stu_id; 列转行后的数据 +--------+--------+--------+-------+ | stu_id | sname | course | score | +--------+--------+--------+-------+ | 1 | 张三 | 语文 | 77 | | 1 | 张三 | 数学 | 88 | | 1 | 张三 | 英语 | 66 | | 2 | 李四 | 语文 | 23 | | 2 | 李四 | 数学 | 45 | | 2 | 李四 | 英语 | 13 | | 3 | 王五 | 英语 | 45 | | 3 | 王五 | 语文 | 94 | | 3 | 王五 | 数学 | 63 | +--------+--------+--------+-------+
b) 行转列
创建一个--学生分数科目表 CREATE TABLE stu_score_pro ( stu_id INT NOT NULL, sname VARCHAR ( 20 ) NOT NULL, course VARCHAR ( 20 ) NOT NULL, score CHAR ( 3 ) NOT NULL ); 表结构 +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | stu_id | int(11) | YES | | NULL | | | sname | varchar(20) | NO | | NULL | | | course | varchar(20) | NO | | NULL | | | score | char(3) | NO | | NULL | | +--------+-------------+------+-----+---------+-------+ 插入数据 INSERT INTO stu_score_pro VALUES ( 1, '张三', '语文', '77' ); INSERT INTO stu_score_pro VALUES ( 1, '张三', '数学', '88' ); INSERT INTO stu_score_pro VALUES ( 1, '张三', '英语', '66' ); INSERT INTO stu_score_pro VALUES ( 2, '李四', '语文', '23' ); INSERT INTO stu_score_pro VALUES ( 2, '李四', '数学', '45' ); INSERT INTO stu_score_pro VALUES ( 2, '李四', '英语', '13' ); INSERT INTO stu_score_pro VALUES ( 3, '王五', '语文', '94' ); INSERT INTO stu_score_pro VALUES ( 3, '王五', '数学', '63' ); INSERT INTO stu_score_pro VALUES ( 3, '王五', '英语', '45' ); 数据列表 +--------+--------+--------+-------+ | stu_id | sname | course | score | +--------+--------+--------+-------+ | 1 | 张三 | 语文 | 77 | | 1 | 张三 | 数学 | 88 | | 1 | 张三 | 英语 | 66 | | 2 | 李四 | 语文 | 23 | | 2 | 李四 | 数学 | 45 | | 2 | 李四 | 英语 | 13 | | 3 | 王五 | 语文 | 94 | | 3 | 王五 | 数学 | 63 | | 3 | 王五 | 英语 | 45 | +--------+--------+--------+-------+ 行转列(正常的行数据根据列中有相同的值转成对应的列) SELECT stu_id, sname, max( CASE WHEN course = '语文' THEN score END ) AS 语文, max( CASE WHEN course = '数学' THEN score END ) AS 数学, max( CASE WHEN course = '英语' THEN score END ) AS 英语 FROM stu_score_pro GROUP BY stu_id, sname; 行转列后的数据 +--------+--------+--------+--------+--------+ | stu_id | sname | 语文 | 数学 | 英语 | +--------+--------+--------+--------+--------+ | 1 | 张三 | 77 | 88 | 66 | | 2 | 李四 | 23 | 45 | 13 | | 3 | 王五 | 94 | 63 | 45 | +--------+--------+--------+--------+--------+