----------------------------------对表的操作------------------------------
1.创建表:
create table student(
id int,
name varchar(32),
age int ,
score double(4,1),
birthday date,
insert_time timestamp
);
2.查看表结构
desc 表名;
3.修改表名
alter table 表名 rename to 新的表名;
4. 添加一列
alter table 表名 add 列名 数据类型;
5. 删除列
alter table 表名 drop 列名;
6. 删除表
drop table 表名;
drop table if exists 表名 ;
----------------------------------数据操作语言--------------------------------
1.增加 insert into
不写列名(所有列全部添加)
insert into 表名 values(值1,值2,...值n);
插入部分数据
insert into 表名(列名1,列名2) values(值1,值2);
2.删除delete
删除表中数据
delete from 表名 where 列名 = 值;
删除表中所有数据
delete from 表名;
3.修改 update
不带条件的修改(会修改所有行)
update 表名 set 列名 = 值;
带条件的修改
update 表名 set 列名 = 值 where 列名=值;
---------------------------------查询类--------------------------------
1.between ... and ...
查询年龄大于等于20 小于等于30
SELECT * FROM student WHERE age >= 20 && age <=30;
SELECT * FROM student WHERE age >= 20 AND age <=30;
SELECT * FROM student WHERE age BETWEEN 20 AND 30;
查询年龄22岁,18岁,25岁的信息
SELECT * FROM student WHERE age = 22 OR age = 18 OR age = 25
SELECT * FROM student WHERE age IN (22,18,25);
2. is null(不为null值)
查询英语成绩不为null
SELECT * FROM student WHERE english IS NOT NULL;
3. like(模糊查询)
_:单个任意字符
%:多个任意字符
查询姓马的有哪些? like
SELECT * FROM student WHERE NAME LIKE '马%';
-查询姓名第二个字是化的人
SELECT * FROM student WHERE NAME LIKE "_化%";
查询姓名是3个字的人
SELECT * FROM student WHERE NAME LIKE '___';
查询姓名中包含德的人
SELECT * FROM student WHERE NAME LIKE '%德%';
4.distinct(去除重复值)
关键词 DISTINCT 用于返回唯一不同的值。
语法:SELECT DISTINCT 列名称 FROM 表名称
第一种去重方式:
SELECT DISTINCT name FROM student ;
第二种去重方式:
SELECT name FROM student GROUP BY name;
5.排序 order by
默认升序
SELECT * FROM person ORDER BY math;
升序第二种方法
SELECT * FROM users ORDER BY username asc;
降序
SELECT * FROM person ORDER BY math desc;
---------------------------------计算类--------------------------------
1.count:计算个数
//使用 COUNT(*)对表中行的数目进行计数时,不管表列中包含的是空值(NULL)还是非空值都会被计数,因为*代表了所有
SELECT COUNT(*) FROM oderlist
//使用 COUNT(column)对特定列中具有值的行进行计数,忽略 NULL 值。
SELECT COUNT(item_price) FROM oderlist
2.max:计算最大值 min:计算最小值
//返回表中的最大单价
SELECT MAX(item_price) AS maxPrice FROM oderlist
//返回表中的最小单价
SELECT MIN(item_price) AS maxPrice FROM oderlist
3.sum:计算和
//SUM()函数忽略列值为 NULL 的行
//返回某一种水果goodsName“火龙果”的销售数量quantity
SELECT SUM(quantity) AS totalQuantity FROM oderlist WHERE goodsName = '火龙果'
4.avg:计算平均数
//求oderlist表格内 item_price 的平均值,返回为avgPrice
SELECT AVG(item_price) avgPrice FROM oderlist
//或者:
SELECT AVG(item_price) AS avgPrice FROM oderlist
//加上条件,返回item_price<10 的 item_price的平均值
SELECT AVG(item_price) AS avgPrice FROM oderlist WHERE item_price <10
5.聚合函数的组合使用:
//查询订单列表中订单量(totalOrders)、最低单价(minItemPrice)、最高单价(maxItemPrice)、均价(avgItemPrice)
SELECT
COUNT(*) AS totalOrders,
MIN(item_price) AS minItemPrice,
MAX(item_price) AS maxItemPrice,
AVG(item_price) AS avgItemPrice
FROM "oderlist"
------------------------------------------------例子:-----------------------------------------------
答案:
select
count(DISTINCT device_id) as did_cnt,
count(id) as question_cnt
from
question_practice_detail
where
date like '2021-08%'