複雜的左連線查詢

2021-09-12 04:28:32 字數 1625 閱讀 7454

一。查詢a表的所有字段,還要查詢和a表關聯的b表的c欄位的個數,條件是a.fnum=b.cnum

在上例中,學習人數和完**數之前的字段都是a表的字段,學習人數和完**數需要關聯查詢b表。

select fcourseware_name,fid ,fcourseware_type ,ftime_long ,flanguages ,fowner ,fcontributor ,fon_line_time,stu.sc,s.c,fstate

from courseware c left join (select count(1) as sc,course_number from user_course_study_info where course_number is not null group by course_number ) stu

on stu.course_number = c.fcourse_id left join (select count(1) as c,course_number from user_course_study_info where browse_score=100 and course_number is not null group by course_number) s

on s.course_number = c.fcourse_id

二。 查a的記錄時,同時要查詢出和a關聯的c表總記錄數,c表不直接和a表關聯,c表和b表是多對一,b和a是多對一。同時也要查出d表和a對應的記錄數,d表和a表關聯。

select * from (select t.fid,

t.fcourse_code,

t.fcourse_name,

t.funit,

t.flevel,

t.fcreatetime,

sum(arr.tc) as tc,

sum(choose.tc2) as tc2

from course_info t left join (select count(1) as tc,

fid,

fcourse_id

from teach_arrangement

group by fid, fcourse_id) arr on arr.fcourse_id = t.fid left join (select count(1) as tc2,

fteach_arrangement_id

from teachplan_student_choose

group by fteach_arrangement_id) choose on choose.fteach_arrangement_id = arr.fid

group by t.fid,

t.fcourse_code,

t.fcourse_name,

t.funit,

t.flevel,

t.fcreatetime) temp left join (select count(1) as tc3,

fcourse_id

from courseware

group by fcourse_id) ware on ware.fcourse_id = temp.fid

查詢結果如下:

複雜的左連線查詢

一。查詢a表的所有字段,還要查詢和a表關聯的b表的c欄位的個數,條件是a.fnum b.cnum 在上例中,學習人數和完 數之前的字段都是a表的字段,學習人數和完 數需要關聯查詢b表。select fcourseware name,fid fcourseware type ftime long fl...

oracle左連線,右連線查詢

oracle左連線,右連線查詢 在oracle pl sql中,左連線和右連線以如下方式來實現 www.2cto.com 檢視如下語句 select emp name,dept name form employee,department where employee.emp deptid depar...

Mysql左連線分頁查詢

個人 1.常見錯誤 select from user t1 left join user tages t2 on t2.userid t1.id limit 0,10 這種先連線查詢在分頁的是錯誤的,正常的是先分頁,再連線查詢 改進 使用子查詢 select t1.id,t1.program tok...