数据库:sql基本语句
内连接,左连接,右连接,自连接的区别:
创建两个表a_table, b_table
左连接: 向左表看齐,返回包括左表中所有的记录和右表中连接字段相等的记录
select * from a_table a left join b_table b on a.id = b.id;
右连接: 向右表看齐,返回包括右边中所有的记录和左表中连接字段相等的记录
select * from a_table a right join b_table b on a.id = b.id;
内连接:只返回两个表中连接字段相等的行
select * from a_table a inner join b_table b on a.id = b.id
自连接Full Join:返回左右两个表所有的记录和左右表中连接字段相等的记录
select * from a_table a full join b_table b on a.id = b.id
https://cxhzt.blog.csdn.net/article/details/81193617?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_antiscanv2&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_antiscanv2&utm_relevant_index=1
https://blog.csdn.net/plg17/article/details/78758593
查询数据库中比平均分大的学生信息:
select *
form table t1
where t1.mark >
( select AVG(mark)
from table t2
where t2.student_id = t1.student_id )
https://blog.csdn.net/Baibair/article/details/108781022?ops_request_misc=&request_id=&biz_id=102&utm_term=%E6%9F%A5%E8%AF%A2%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B8%AD%E6%AF%94%E5%B9%B3%E5%9D%87%E5%88%86%E5%A4%A7%E7%9A%84%E5%AD%A6%E7%94%9F%E4%BF%A1%E6%81%AF&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-5-108781022.142^v5^article_score_rank,143^v6^control&spm=1018.2226.3001.4187
查询有几个不同的职位名称:
select distinct name from a;
group by:
现在有两个表product和sales,字段分别如下:
TABLES:product
字段:id(产品id)、Name(产品名称)
TABLES:sales
字段:id(销售id)、product_id(产品id)、DATE(日期)、amount(销售数量)
1、修改产品D的产品名称为E
update product set Name=E where Name=D ;
2、插入产品名称为“产品E”的产品数据;
insert into table(product) field(产品E);
3、删除产品名称为"产品E"的数据;
delete from product where Name = 产品E;
4、查询2014-1-1至2014-1-3期间单日销售数量大于110的产品;
select name from product a, sales b where a.date > 2014.1.1 and < 2014.1.3 b.amount >110;
5、查询销售总量最多的产品名称;
select Name from product a,sales b where amount=max(amount);
6、查询每种分类的总和:查询每种产品的销售总量;
select sum(amount) from product a, sales b group by b.proudct_id;
7、查询销售总量大于300的产品;
select id Name from product a right join sales b where having sum(amount)>300;
---------------------------------------------------------
1、查询多少个产品;
select distinct Name from product;
2、统计产品的个数;
select count(distinct name) from product;