------------------------------- Operation on the table ------------ ------------------
1. Create a table:
create table student(
id int,
name varchar(32),
age int ,
score double(4,1),
birthday date,
insert_time timestamp
);
2. View table structure
desc table name;
3. Modify table name
alter table table name rename to new table name;
4. Add a column
alter table table name add column name data type;
5. Delete column
alter table table name drop column name ;
6. Delete table
drop table table name;
drop table if exists table name;
-------------------------------Data Manipulation Language------------- -------------------
1. Increase insert into
without writing column names (all columns are added)
insert into table name values (value 1, value 2, ... value n);
insert some data
insert into table name (column name 1, column name 2) values(value1, value2);
2. delete delete
Delete data in the table
delete from table name where column name = value;
delete all data in the table
delete from table name;
3. Modify update
Unconditional modification (all rows will be modified)
update table name set column name = value;
conditional modification
update table name set column name = value where column name = value;
---------------------------------Query class --------------- -----------------
1.between ... and ...
Query age greater than or equal to 20 and less than or equal to 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;
Query age information of 22, 18, and 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 (not a null value)
Query English score is not null
SELECT * FROM student WHERE english IS NOT NULL;
3. like (fuzzy query) _: single arbitrary character %: multiple arbitrary characters Who are the ones with the surname Ma? like SELECT * FROM student WHERE NAME LIKE '马%'; - query the person whose name the second word is Hua SELECT * FROM student WHERE NAME LIKE "_化%"; query the person whose name is 3 characters SELECT * FROM student WHERE NAME LIKE '___'; Query people whose name contains Germany SELECT * FROM student WHERE NAME LIKE '%德%';
4.distinct (remove duplicate values)
The keyword DISTINCT is used to return uniquely different values.
Syntax: SELECT DISTINCT column name FROM table name
The first deduplication method:
SELECT DISTINCT name FROM student ;
The second deduplication method:
SELECT name FROM student GROUP BY name;
5. sort order by
Default ascending
SELECT * FROM person ORDER BY math;
ascending second method
SELECT * FROM users ORDER BY username asc;
降序
SELECT * FROM person ORDER BY math desc;
--------------------------------- Computing class --------------- -----------------
1.count: Calculate the number
//使用 COUNT(*)对表中行的数目进行计数时,不管表列中包含的是空值(NULL)还是非空值都会被计数,因为*代表了所有
SELECT COUNT(*) FROM oderlist
//使用 COUNT(column)对特定列中具有值的行进行计数,忽略 NULL 值。
SELECT COUNT(item_price) FROM oderlist
2.max: Calculate the maximum value min: Calculate the minimum value
//返回表中的最大单价
SELECT MAX(item_price) AS maxPrice FROM oderlist
//返回表中的最小单价
SELECT MIN(item_price) AS maxPrice FROM oderlist
3.sum: calculate and
//SUM()函数忽略列值为 NULL 的行
//返回某一种水果goodsName“火龙果”的销售数量quantity
SELECT SUM(quantity) AS totalQuantity FROM oderlist WHERE goodsName = '火龙果'
4.avg: Calculate the average
//求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. Combination of aggregate functions:
//查询订单列表中订单量(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"
------------------------------------------------example: -----------------------------------------------
Answer:
select
count(DISTINCT device_id) as did_cnt,
count(id) as question_cnt
from
question_practice_detail
where
date like '2021-08%'