mysql

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 连接相关操作:   #mysql -uroot -p   #show databases;   #use   XXX_database;   #show tables; 建表语句: create table 表名(列名称,列类型 [列属性][默认值]), engine 引擎名 char...

连接相关操作:

  #mysql -uroot -p

  #show databases;

  #use   XXX_database;

  #show tables;

建表语句:

create table 表名(列名称,列类型 [列属性][默认值]),
engine 引擎名 charset 字符集

增:
往那张表增,增哪几列,各为什么值;
insert into 表名 (列1,列2, ... 列n)
values
(值1, 值2.......值N)

如果不声明拆入的列,则默认拆入所有列;

改:update
修改哪张表,修改哪几列,修改成什么值?
在哪几行上生效

update 表名
set
列1 = 值1
列2 = 值2
.。。
列N = 值N

where 表达式


delete:
删除哪几张表的数据,删那些行

delete from 表名
where 表达式

查:
select * from 表名

查询5种子句:where 后面的表示式子代入到每行,确认是否成立;
where shop_price - market_price > 200;

in(值1, 值2, 值3, ...值N)等于值1-N任意之一,都可以;
select good_id, cat_id from goods where cat_id in (4, 5);

between 在某一范围内;
between 值1 and 值2, 表示在值1和值2之间(允许等于边界);
select good_it, cat_id from goods where cat_id between 1 and 6;

or用法
select good_id, good_name, shop_price from goods where shop_price >=3000
and shop_price <=5000 or shop_price >=500 and shop_price<=1000;

not的用法:
select good_id, cat_id from goods where cat_id not in (4, 5);
select good_id, cat_id from goods where cat_id!=4 and cat_id != 5;

模糊查询:‘ % ’:通配任意字符 '_':单个字符
select good_id, cat_id from goods where good_name like '%诺基亚%';

group by (要和聚合函数(统计函数)一起使用)
作用:把行 按 字段 分组
语法:group by col1, col2, ... colN
运用场合:
常见于统计场合,如按栏目计数帖子数,
统计每个人的平均成绩等;

max(shop_price) //聚合函数

select good_id, good_name, max(shop_price) from goods; //语法是错误的
select min(shop_price) from goods; //ok

select cat_id, max(shop_price) from goods group by cat_id;

select min(shop_price) from goods;

select min(goods_id) from goods;

select sum(goods_number) from goods;

select avg(shop_price) from goods;

计算表中函数
select count(*) from goods;

select cat_id, min(shop_price) from goods group by cat_id; //ok

select cat_id, count(*) from goods group by cat_id;

select good_id, good_name, market_price-shop_price from goods;


select cat_id, sum(shop_price *good_number) from goods group by cat_id;

给列取别名: as

select cat_id, sum(shop_price*goods_number) as huokuan from goods group by cat_id;

mysql连接查询: 

select test1.id, test1.name, test1.address, test2.age from test1, test2 where test2.age=test1.id;
等价于:(使用内连接)
select test1.id, test1.name, test1.address, test2.age from test1 inner join test2 on test1.id=test2.age;

inner join(内连接):
inner join:如果表中有至少一个匹配,则返回行;
left join:左连接,即使右表中没有匹配,也从左表返回所有的行;
select test1.id, test2.name, test1.address, test2.age from test1 left join test2 on
test1.id=test2.age;
(右表: test2, 左表:test1)

right join: 右连接,即使左表中没有匹配,也从右表返回所有的行;
select test1.id, test1.name, test1.address, test2.age from test1 right join test2 on
test1.id = test2.age;
(右表:test2 左表:test1)

full join: 只要其中一个表中存在匹配,就返回行;
select test1.id, test1.name, test1.address, test2.age from test1 right join test2 on
test1.id = test2.age;

union:联合统计结果
select name from test1 where age between 25 and 50 union select name from test2 where age<100;

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
8天前
|
SQL 关系型数据库 MySQL
初学mysql
本文档介绍了MYSQL中的表格与键的概念,包括列、行、主键和外键。接着,展示了SQL语法基础,如创建、查看、删除数据库及操作表格。讨论了不同数据类型,如decimal、varchar、blob等。通过示例说明如何添加、删除列,插入、更新和删除数据,以及查询技巧,如使用WHERE、ORDER BY和LIMIT子句。
9 0
|
4月前
|
存储 关系型数据库 MySQL
mysql(下)
mysql(下)
46 0
|
5月前
|
关系型数据库 MySQL
MySQL问题汇总
MySQL问题汇总
35 0
|
7月前
|
SQL 分布式计算 关系型数据库
MySql为什么要用where 1=1和where 1<>1
MySql为什么要用where 1=1和where 1<>1
45 0
|
9月前
|
SQL 存储 缓存
|
10月前
|
SQL 关系型数据库 MySQL
盘点一下Mysql中的一些小知识(二)
盘点一下Mysql中的一些小知识(二)
61 0
|
11月前
|
存储 关系型数据库 MySQL
出现MySQL相关的问题
出现MySQL相关的问题
86 0
|
SQL 关系型数据库 MySQL
MySQL5.7及以上 转 MySQL5.5
MySQL5.7及以上 转 MySQL5.5
119 0
|
关系型数据库 MySQL
MySql 时间查询
MySql 时间查询
|
关系型数据库 MySQL 数据库
MySQL(四)
MySQL(四),一起来学习吧。

热门文章

最新文章