0
点赞
收藏
分享

微信扫一扫

中间件 | Kafka - [常见问题]

独孤凌雪 2024-03-17 阅读 32

文章目录

一、表查询

1.表字段的操作

①查看表结构
desc 表名;
# 查看表中的字段类型,名字,长度,约束
desc studenttest;
②字段的增加
# 默认在表末尾增加一个字段
alter table 表名 add 字段名 数据类型;

# 添加到某个字段之后
alter table 表名 add 字段名 数据类型 after 字段名(被指定);

# 第一个添加字段,
alter table 表名 add 字段名 数据类型 first;

alter table studenttest add age int(3) ;

alter table studenttest add s_height double(5,2) after s_name;
desc studenttest;

alter table studenttest add first_id int(5) first;
desc studenttest;
③字段长度/数据类型的修改
# 格式
alter table studenttest modify column 字段名 数据类型(长度);

# 修改长度注意一定要大于原来长度,原有数据会被破坏,不可修改
# 字符串类型改小一点会报错的
alter table studenttest modify column s_name char(1);
desc studenttest;	
④字段名的修改
alter table 表名 change 旧的字段名 新的字段名 数据类型;
alter table studenttest change s_name Name char(50);
⑤删除字符段
alter table 表名 drop column 字段名;
alter table studenttest drop column first_id;
⑥清空表数据

#只是清空数据,表的结构还在

delete from 表名;
delete from studenttest;
⑦修改表名
alter table 表名 rename 新的表名;
alter table studenttest rename stu;
⑧删除表
drop table 表名;
drop table stu;

2、表数据查询

# 格式:* 就是全部的意思
select * from 表名;


# 查看指定的字段所有数据
select * from xiaoheizi;

select name from xiaoheizi;

select name, age from xiaoheizi;

3、where 字段

where 条件附加

比较运算符:

=
!=


<
=
<=

逻辑运算符

and
or
not

还有其他mysql特有的知识
between 在两个值之间
not betwenn 不在两个值之间
in 在指定集合之中
not in 不在指定集合之中

# 格式
select * from 表名 where 条件语句;


select 字段名1,.... from 表名 where 条件语句;
select name from xiaoheizi where age > 18 ;

# 查询年龄在19-21之间的名字
select name from xiaoheizi where age BETWEEN 19 and 28;


# 年龄在18,16,22之中的名字
select name from xiaoheizi where age in (16,18,22);

4、聚合函数

聚合函数就是: sum, max,min,avg

聚合函数可以引用到select查询中,或者having子句中,但是不能用在where子句中,因为where是对行记录逐条筛选的



# 求平均值
avg(字段名)

# 统计一个字段个数
count(字段名)


select avg(age) from xiaoheizi;
select max(age) from xiaoheizi;
select min(age) from xiaoheizi;
select sum(age) from xiaoheizi;
select count(age) from xiaoheizi;
举报

相关推荐

0 条评论