mysql基本语法「建议收藏」

mysql基本语法「建议收藏」建库建表Demo — 建库 create DATABASE db_book; use db_book; — 建表 CREATE TABLE t_bookType( id int primary k

mysql基本语法

建库建表Demo
mysql基本语法「建议收藏」

-- 建库
create DATABASE db_book;
use db_book;
-- 建表
CREATE TABLE t_bookType(
    id int primary key auto_increment,
    bookTypeName varchar(20),
    bookTypeDesc varchar(200)
);
CREATE TABLE t_book(
    id int primary key auto_increment,
    bookName varchar(20),
    author varchar(10),
    price decimal(6,2),
    bookTypeId int,
    constraint `fk` foreign key (`bookTypeId`) references `t_bookType`(`id`)
);
-- 查看表结构
desc t_bookType;
-- 查看表ddl(建表语句)
show create table t_bookType;
-- 重命名表
alter table t_book rename t_book2;

代码100分

View Code

建立单表
mysql基本语法「建议收藏」

代码100分-- 建表
create table `t_student` (
    `id` double ,
    `stuName` varchar (60),
    `age` double ,
    `sex` varchar (30),
    `gradeName` varchar (60)
); 
-- 插入记录
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("1","张一","23","","一年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("2","张二","25","","二年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("3","张三","23","","一年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("4","张四","22","","三年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("5","张五","21","","一年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("6","李一","26","","二年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("7","李二","20","","三年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("8","李三","21","","二年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("9","李四","22","","一年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("10","李五","25","","二年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("11","小黑","21",NULL,"二年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("12","小白","23","","二年级");
insert into `t_student` (`id`, `stuName`, `age`, `sex`, `gradeName`) values("13","小红","24",NULL,"二年级");

View Code

简单的单表查询
mysql基本语法「建议收藏」

-- 查询
SELECT id,stuName,age,sex,gradeName FROM t_student ;
SELECT * FROM t_student;
SELECT * FROM t_student WHERE id=1;
SELECT * FROM t_student WHERE age>22;
-- in 相当于集合吧,别和between混淆
SELECT * FROM t_student WHERE age IN (21,22,23);
SELECT * FROM t_student WHERE age NOT IN (21,23);
-- [21,24]
SELECT * FROM t_student WHERE age BETWEEN 21 AND 24;
SELECT * FROM t_student WHERE age NOT BETWEEN 21 AND 24;
-- 模糊查询
SELECT * FROM t_student WHERE stuName LIKE "张三";
SELECT * FROM t_student WHERE stuName LIKE "张%";
SELECT * FROM t_student WHERE stuName LIKE "%张%";
-- 交集
SELECT * FROM t_student WHERE gradeName="一年级" AND age=23;
-- 并集
SELECT * FROM t_student WHERE gradeName="一年级" OR age=23;
-- DISTINCT去重
SELECT DISTINCT gradeName FROM t_student;
-- 升序
SELECT * FROM t_student ORDER BY age ASC;
-- 降序
SELECT * FROM t_student ORDER BY age DESC;
-- 分组查询
SELECT gradeName,GROUP_CONCAT(stuName) FROM t_student GROUP BY gradeName;
-- 分页查询(index,size)
SELECT * FROM t_student LIMIT 2,5;

View Code

代码100分再建单表

mysql基本语法「建议收藏」

create table `t_grade` (
    `id` int ,
    `stuName` varchar (60),
    `course` varchar (60),
    `score` int 
); 
insert into `t_grade` (`id`, `stuName`, `course`, `score`) values("1","张三","语文","91");
insert into `t_grade` (`id`, `stuName`, `course`, `score`) values("2","张三","数学","90");
insert into `t_grade` (`id`, `stuName`, `course`, `score`) values("3","张三","英语","87");
insert into `t_grade` (`id`, `stuName`, `course`, `score`) values("4","李四","语文","79");
insert into `t_grade` (`id`, `stuName`, `course`, `score`) values("5","李四","数学","95");
insert into `t_grade` (`id`, `stuName`, `course`, `score`) values("6","李四","英语","80");
insert into `t_grade` (`id`, `stuName`, `course`, `score`) values("7","王五","语文","77");
insert into `t_grade` (`id`, `stuName`, `course`, `score`) values("8","王五","数学","81");
insert into `t_grade` (`id`, `stuName`, `course`, `score`) values("9","王五","英语","89");

View Code

mysql基本语法「建议收藏」

-- 聚合查询,还是分组聚合比较多
SELECT COUNT(*) FROM t_grade;
SELECT stuName,COUNT(*) FROM t_grade GROUP BY stuName;
SELECT stuName,SUM(score) FROM t_grade GROUP BY stuName;
SELECT stuName,AVG(score) FROM t_grade WHERE stuName="张三";
SELECT stuName,AVG(score) FROM t_grade GROUP BY stuName;

View Code

建立无外键的俩表
mysql基本语法「建议收藏」

USE `db_book`;
DROP TABLE IF EXISTS `t_book`;
CREATE TABLE `t_book` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `bookName` varchar(20) DEFAULT NULL,
  `price` decimal(6,2) DEFAULT NULL,
  `author` varchar(20) DEFAULT NULL,
  `bookTypeId` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
insert  into `t_book`(`id`,`bookName`,`price`,`author`,`bookTypeId`) values (1,"Java编程思想","100.00","埃史尔",1),(2,"Java从入门到精通","80.00","李钟尉",1),(3,"三剑客","70.00","大仲马",2),(4,"生理学(第二版)","24.00","刘先国",4);
DROP TABLE IF EXISTS `t_booktype`;
CREATE TABLE `t_booktype` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `bookTypeName` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

insert  into `t_booktype`(`id`,`bookTypeName`) values (1,"计算机类"),(2,"文学类"),(3,"教育类");

View Code

多表查询(俩表)
mysql基本语法「建议收藏」

-- 笛卡尔积
SELECT * FROM t_book,t_bookType;
SELECT * FROM t_book,t_bookType WHERE t_book.bookTypeId=t_bookType.id;
SELECT bookName,author,bookTypeName FROM t_book,t_bookType WHERE t_book.bookTypeId=t_bookType.id;
SELECT tb.bookName,tb.author,tby.bookTypeName FROM t_book tb,t_bookType tby WHERE tb.bookTypeId=tby.id;
-- 返回左表所有记录,哪怕右表为空
SELECT * FROM t_book LEFT JOIN t_bookType ON t_book.bookTypeId=t_bookType.id;
-- 返回右表所有记录,哪怕左表为空
SELECT * FROM t_book RIGHT JOIN t_bookType ON t_book.bookTypeId=t_bookType.id;
SELECT tb.bookName,tb.author,tby.bookTypeName FROM t_book tb LEFT JOIN t_bookType tby ON tb.bookTypeId=tby.id;
SELECT tb.bookName,tb.author,tby.bookTypeName FROM t_book tb,t_bookType tby WHERE tb.bookTypeId=tby.id AND tb.price>70;

View Code

建表子查询
mysql基本语法「建议收藏」

create table `t_pricelevel` (
    `id` int ,
    `priceLevel` int ,
    `price` float ,
    `description` varchar (300)
); 
insert into `t_pricelevel` (`id`, `priceLevel`, `price`, `description`) values("1","1","80.00","价格贵的书");
insert into `t_pricelevel` (`id`, `priceLevel`, `price`, `description`) values("2","2","60.00","价格适中的书");
insert into `t_pricelevel` (`id`, `priceLevel`, `price`, `description`) values("3","3","40.00","价格便宜的书");
-- 子查询
SELECT * FROM t_book WHERE booktypeId IN (SELECT id FROM t_booktype);
SELECT * FROM t_book WHERE booktypeId NOT IN (SELECT id FROM t_booktype);
SELECT * FROM t_book WHERE price>=(SELECT price FROM t_pricelevel WHERE priceLevel=1);
SELECT * FROM t_book WHERE EXISTS (SELECT * FROM t_booktype);
SELECT * FROM t_book WHERE NOT EXISTS (SELECT * FROM t_booktype);
SELECT * FROM t_book WHERE price>= ANY (SELECT price FROM t_pricelevel);
SELECT * FROM t_book WHERE price>= ALL (SELECT price FROM t_pricelevel);

View Code

博客使用的mysql实例均来自http://www.java1234.com/

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/9685.html

(0)
上一篇 2023-02-03
下一篇 2023-02-03

相关推荐

  • Chaos Mesh® 正式进入 CNCF 沙箱托管项目[亲测有效]

    Chaos Mesh® 正式进入 CNCF 沙箱托管项目[亲测有效]近日,云原生计算基金会 (CNCF) 宣布云原生的混沌工程 Chaos Mesh 正式进入 CNCF 沙箱托管项目,这是 CNCF 接纳的第二个由 PingCAP 团队设计并研发的项目。 Chaos…

    2023-03-24
    140
  • Python List转Array的小技巧

    Python List转Array的小技巧Python是一种强大的编程语言,广泛应用于各种领域。Python List是Python语言中的一种基本数据类型,可以用于存储一系列的数据。然而,在某些情况下,我们需要将Python List转换为Array类型,以便更好地执行我们的程序。在本文中,我们将介绍Python List转Array的小技巧,让读者可以更好地理解这个过程,并使他们的代码更为高效。

    2024-05-04
    71
  • 【SQLite】教程06-SQLite表操作

    【SQLite】教程06-SQLite表操作创建表: CREATE TABLE 语句用于在任何给定的数据库创建一个新表。命名表、定义列、定义每一列的数据类型 查看表: 详细查看表: 重命名表: 删除表: 创建表并添加7条记录(第七条记录用了第…

    2023-03-31
    143
  • 436分排名_好分数怎样看年级排名

    436分排名_好分数怎样看年级排名题目描述 编写一个 SQL 查询来实现分数排名。 如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。 + + +

    2023-03-28
    159
  • 用一行Python代码将列表内容打印成字符串

    用一行Python代码将列表内容打印成字符串Python中的join方法可以将列表中的元素拼接成字符串,而不需要使用循环。我们可以直接使用join将列表内容打印出来。

    2024-03-28
    73
  • 使用Python爬取网页数据的方法

    使用Python爬取网页数据的方法在当今互联网时代,获取网页数据已经成为了一项非常关键的任务。而Python作为当今非常流行的编程语言之一,拥有强大的网络爬虫库。本文将介绍使用Python爬取网页数据的方法。

    2024-09-12
    29
  • SQL99相较于SQL92在多表查询时的新语法「建议收藏」

    SQL99相较于SQL92在多表查询时的新语法「建议收藏」1.自然连接 NATURAL JOIN SQL99中新增的自然连接相当于SQL92中的等值连接。它可以自动的查询两个表中所有的相同字段,然后进行等值连接。 在SQL92中: SELECT 表1.字段1

    2023-05-28
    149
  • CentOS7安装Elasticsearch7.6.2「建议收藏」

    CentOS7安装Elasticsearch7.6.2「建议收藏」最近登录到Elasticsearch官网发现Elasticsearch已经更新到7.8的版本了,但是自己公司使用的还是6.4版本,查了下,发现7.X的版本与6.X的版本差别还挺大的,所以准备在自己的个

    2023-03-28
    168

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注