SQL 三個表聯接的join寫法

2022-09-20 21:22:10 字數 4857 閱讀 3948

select

university,

difficult_level,

count(qpd.question_id) /

count(distinct qpd.device_id) as

**g_answer_cnt

from question_practice_detail as

qpdleft

join user_profile as

upon up.device_id=

qpd.device_id

left

join question_detail as

qdon qd.question_id=

qpd.question_id

group

by university, difficult_level

資料:統計每個學校各難度的使用者平均刷題數

關鍵字:每個,各,不同,用group by

drop

table

ifexists

`user_profile`;

drop

table

ifexists

`question_practice_detail`;

drop

table

ifexists

`question_detail`;

create

table

`user_profile` (

`id`

intnot

null

,`device_id`

intnot

null

,`gender`

varchar(14) not

null

,`age`

int,

`university`

varchar(32) not

null

,`gpa`

float

,`active_days_within_30`

int,

`question_cnt`

int,

`answer_cnt`

int);

create

table

`question_practice_detail` (

`id`

intnot

null

,`device_id`

intnot

null

,`question_id`

intnot

null

,`result`

varchar(32) not

null

);create

table

`question_detail` (

`id`

intnot

null

,`question_id`

intnot

null

,`difficult_level`

varchar(32) not

null

);insert

into user_profile values(1,2138,'

male

',21,'

北京大學

',3.4,7,2,12

);insert

into user_profile values(2,3214,'

male

',null,'

復旦大學

',4.0,15,5,25

);insert

into user_profile values(3,6543,'

female

',20,'

北京大學

',3.2,12,3,30

);insert

into user_profile values(4,2315,'

female

',23,'

浙江大學

',3.6,5,1,2

);insert

into user_profile values(5,5432,'

male

',25,'

山東大學

',3.8,20,15,70

);insert

into user_profile values(6,2131,'

male

',28,'

山東大學

',3.3,15,7,13

);insert

into user_profile values(7,4321,'

male

',28,'

復旦大學

',3.6,9,6,52

);insert

into question_practice_detail values(1,2138,111,'

wrong');

insert

into question_practice_detail values(2,3214,112,'

wrong');

insert

into question_practice_detail values(3,3214,113,'

wrong');

insert

into question_practice_detail values(4,6543,111,'

right');

insert

into question_practice_detail values(5,2315,115,'

right');

insert

into question_practice_detail values(6,2315,116,'

right');

insert

into question_practice_detail values(7,2315,117,'

wrong');

insert

into question_practice_detail values(8,5432,117,'

wrong');

insert

into question_practice_detail values(9,5432,112,'

wrong');

insert

into question_practice_detail values(10,2131,113,'

right');

insert

into question_practice_detail values(11,5432,113,'

wrong');

insert

into question_practice_detail values(12,2315,115,'

right');

insert

into question_practice_detail values(13,2315,116,'

right');

insert

into question_practice_detail values(14,2315,117,'

wrong');

insert

into question_practice_detail values(15,5432,117,'

wrong');

insert

into question_practice_detail values(16,5432,112,'

wrong');

insert

into question_practice_detail values(17,2131,113,'

right');

insert

into question_practice_detail values(18,5432,113,'

wrong');

insert

into question_practice_detail values(19,2315,117,'

wrong');

insert

into question_practice_detail values(20,5432,117,'

wrong');

insert

into question_practice_detail values(21,5432,112,'

wrong');

insert

into question_practice_detail values(22,2131,113,'

right');

insert

into question_practice_detail values(23,5432,113,'

wrong');

insert

into question_detail values(1,111,'

hard');

insert

into question_detail values(2,112,'

medium');

insert

into question_detail values(3,113,'

easy');

insert

into question_detail values(4,115,'

easy');

insert

into question_detail values(5,116,'

medium');

insert

into question_detail values(6,117,'

easy

');

JOIN 在ACESS與SQL的不同寫法

多個left join 不在access 和sql的寫法是不同的 如 access select g.ccbh,g.jyrq,g.10ib老化後 g.033ib老化後 g.05lib老化後 h.05l02ib老化後 from select e.ccbh,e.jyrq,e.10ib老化後 e.033i...

tcp socket修改三個表

insert into mes adapter adapterid,adaptertype,adaptername,adapterstate,createdate,updatedate,techname,endname,description,packagename,srfun,rfun,macfu...

Sql語言 組合兩個表 join

sql中常用的連線方式 組合兩個 兩表中至少存在乙個匹配時,返回匹配項兩表合併的內容。select column name s from table name1 inner join table name2 on table name1.column name table name2.column ...