mysql join语句在线练习

Posted master-dragon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql join语句在线练习相关的知识,希望对你有一定的参考价值。

目录

http://mysql.jsrun.net/

CREATE DATABASE test;
use test;
CREATE TABLE student (
	id int,
	name varchar(255),
	address varchar(255),
	city varchar(255)
);
INSERT INTO student VALUES (1, '刘一', '郑十家隔壁', '河南');
INSERT INTO student VALUES (2, '陈二',  '李四家隔壁', '安徽');
INSERT INTO student VALUES (3, '张三',  '白娘子家隔壁', '杭州');
INSERT INTO student VALUES (4, '李四',  '许仙家隔壁', '杭州');
INSERT INTO student VALUES (5, '王五',  '李四家隔壁', '杭州');
INSERT INTO student VALUES (6, '赵六',  '赵六家隔壁', '杭州');
INSERT INTO student VALUES (7, '孙七',  '张三家隔壁', '杭州');
INSERT INTO student VALUES (8, '周八',  '雷峰塔附近', '杭州');
INSERT INTO student VALUES (9, '吴九',  '孙七家隔壁', '杭州');
INSERT INTO student VALUES (10, '郑十',  '周八家隔壁', '杭州');
 
CREATE TABLE stu_course (
	stu_id int,
	course_name varchar(255)
);
INSERT INTO stu_course VALUES (3, '语文');
INSERT INTO stu_course VALUES (3, '数学');
INSERT INTO stu_course VALUES (3, '英语');
INSERT INTO stu_course VALUES (4, '语文');
INSERT INTO stu_course VALUES (4, '体育');
INSERT INTO stu_course VALUES (5, '历史');
INSERT INTO stu_course VALUES (5, '体育');
INSERT INTO stu_course VALUES (11, '历史');
INSERT INTO stu_course VALUES (11, '体育');

笛卡尔积:CROSS JOIN

inner join


默认join:

left join

right join

例子:count按时间累加

CREATE DATABASE test;
use test;
CREATE TABLE people (
	year varchar(255),
	number int
);
INSERT INTO people VALUES ('2018', 1);
INSERT INTO people VALUES ('2019', 3);
INSERT INTO people VALUES ('2020', 5);
INSERT INTO people VALUES ('2021', 9);


select a.year as year, sum(b.number) as number
from people a  
	join people b 
	on a.year >= b.year
group by year
order by year

累加如下:

select 
    p1.year, 
    p1.number as number,
    p2.number as sum_number
from people p1 
left join
(
    select a.year as year, sum(b.number) as number
    from people a 
    left join people b 
    on a.year >= b.year
    group by  a.year
) p2 
on p1.year = p2.year

以上是关于mysql join语句在线练习的主要内容,如果未能解决你的问题,请参考以下文章

mysql练习----More JOIN operations

MySQL数据库_(学生_教师_课程_成绩)表_练习

Mysql的join语句

Mysql Join-连接查询(中)

MYSQL数据库SQL语句练习实验

MYSQL数据库SQL语句练习实验