博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql建表sql
阅读量:2489 次
发布时间:2019-05-11

本文共 4636 字,大约阅读时间需要 15 分钟。

mysql建表

文章目录

mysql学生表

CREATE TABLE `courses` (`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '自增id',`student` VARCHAR(255) DEFAULT NULL COMMENT '学生',`class` VARCHAR(255) DEFAULT NULL COMMENT '课程',`score` INT(255) DEFAULT NULL COMMENT '分数',PRIMARY KEY (`id`),UNIQUE KEY `course` (`student`, `class`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;

插入数据

INSERT INTO `courses`(`student`, `class`, `score`) VALUES('A', 'Math', 90);INSERT INTO `courses`(`student`, `class`, `score`) VALUES('A', 'Chinese', 80);INSERT INTO `courses`(`student`, `class`, `score`) VALUES('A', 'English', 70);INSERT INTO `courses`(`student`, `class`, `score`) VALUES('A', 'History', 80);INSERT INTO `courses`(`student`, `class`, `score`) VALUES('B', 'Math', 73);INSERT INTO `courses`(`student`, `class`, `score`) VALUES('B', 'Chinese', 60);INSERT INTO `courses`(`student`, `class`, `score`) VALUES('B', 'English', 70);INSERT INTO `courses`(`student`, `class`, `score`) VALUES('B', 'History', 90);INSERT INTO `courses`(`student`, `class`, `score`) VALUES('C', 'Math', 70);INSERT INTO `courses`(`student`, `class`, `score`) VALUES('C', 'Chinese', 50);INSERT INTO `courses`(`student`, `class`, `score`) VALUES('C', 'English', 20);INSERT INTO `courses`(`student`, `class`, `score`) VALUES('C', 'History', 10);INSERT INTO `courses`(`student`, `class`, `score`) VALUES('D', 'Math', 53);INSERT INTO `courses`(`student`, `class`, `score`) VALUES('D', 'Chinese', 32);INSERT INTO `courses`(`student`, `class`, `score`) VALUES('D', 'English', 99);INSERT INTO `courses`(`student`, `class`, `score`) VALUES('D', 'History', 100);

建表,学生和id

CREATE TABLE `phone` (`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '自增id',`student` VARCHAR(255) DEFAULT NULL COMMENT '学生',`phone` VARCHAR(255) DEFAULT NULL COMMENT '电话',PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;

group by

mysql> select * from courses;+----+---------+---------+-------+| id | student | class   | score |+----+---------+---------+-------+|  1 | A       | Math    |    90 ||  2 | A       | Chinese |    80 ||  3 | A       | English |    70 ||  4 | A       | History |    80 ||  5 | B       | Math    |    73 ||  6 | B       | Chinese |    60 ||  7 | B       | English |    70 ||  8 | B       | History |    90 ||  9 | C       | Math    |    70 || 10 | C       | Chinese |    50 || 11 | C       | English |    20 || 12 | C       | History |    10 || 13 | D       | Math    |    53 || 14 | D       | Chinese |    32 || 15 | D       | English |    99 || 16 | D       | History |   100 |+----+---------+---------+-------+16 rows in set (0.00 sec)mysql> select class, min(score) from courses group by class;+---------+------------+| class   | min(score) |+---------+------------+| Math    |         53 || Chinese |         32 || English |         20 || History |         10 |+---------+------------+4 rows in set (0.00 sec)mysql> select class from courses group by class;+---------+| class   |+---------+| Chinese || English || History || Math    |+---------+4 rows in set (0.00 sec)

inner join

mysql> select *  from phone;+----+---------+-------------+| id | student | phone       |+----+---------+-------------+|  1 | A       | 123123123   ||  2 | B       | 12312312322 ||  3 | C       | 321321      |+----+---------+-------------+3 rows in set (0.00 sec)mysql> select *  from courses;+----+---------+---------+-------+| id | student | class   | score |+----+---------+---------+-------+|  1 | A       | Math    |    90 ||  2 | A       | Chinese |    80 ||  3 | A       | English |    70 ||  4 | A       | History |    80 ||  5 | B       | Math    |    73 ||  6 | B       | Chinese |    60 ||  7 | B       | English |    70 ||  8 | B       | History |    90 ||  9 | C       | Math    |    70 || 10 | C       | Chinese |    50 || 11 | C       | English |    20 |+----+---------+---------+-------+11 rows in set (0.00 sec)mysql> select a.phone, b.class from phone a inner join courses b on a.student= b.student;+-------------+---------+| phone       | class   |+-------------+---------+| 123123123   | Chinese || 123123123   | English || 123123123   | History || 123123123   | Math    || 12312312322 | Chinese || 12312312322 | English || 12312312322 | History || 12312312322 | Math    || 321321      | Chinese || 321321      | English || 321321      | Math    |+-------------+---------+11 rows in set (0.00 sec)mysql> select distinct a.phone, b.student from phone a inner join courses b on a.student= b.student;+-------------+---------+| phone       | student |+-------------+---------+| 123123123   | A       || 12312312322 | B       || 321321      | C       |+-------------+---------+3 rows in set (0.00 sec)

转载地址:http://nzorb.baihongyu.com/

你可能感兴趣的文章
php获取当前时间戳方法
查看>>
JAVA字符串格式化-String.format()的使用
查看>>
编译时类型 和运行时类型的 区别(1)
查看>>
php pcntl 多进程学习
查看>>
C++类指针类型的成员变量的浅复制与深复制
查看>>
看看清华的同学在四年的大学中干什么吧,非常值得学习
查看>>
Java和.NET(C#)的开发用到的技术对比总结
查看>>
关于strassen矩阵乘法的矩阵大小不是2^k的形式时,时间复杂度是否还是比朴素算法好的看法...
查看>>
vl_sift函数用法
查看>>
Ios17个常用代码整理
查看>>
适配ios7
查看>>
项目复审——Beta阶段
查看>>
Android 实现切换主题皮肤功能(类似于众多app中的 夜间模式,主题包等)
查看>>
在Android App中集成Google登录
查看>>
openstack quantum搭建过程中一些有用的链接
查看>>
数据库:mysql 获取刚插入行id[转]
查看>>
Egret入门学习日记 --- 第二篇
查看>>
前端“黑话”polyfill
查看>>
wifi-mac
查看>>
linux 下运行 tomcat
查看>>