mysql 中LEFT JOIN基本用法例項

2021-06-29 14:13:15 字數 1324 閱讀 4128

/*sql 中left join的含義是__,如果 tbl_user記錄了學生的姓名(name)和學號(id),

tbl_score記錄了學生(有的學生考試以後被開除了,沒有其記錄)的學號(id)和考試成績(score)以及考試科目(subject),要想列印出各個學生姓名及對應的的各科總成績,則可以用sql語句____.

答:自然左外連線

*/

create database phpinterview;

use phpinterview

create table tbl_user

(id                             int                            not null,

name                           varchar(50)                    not null,

primary key (id)

);create table tbl_score

(id                             int                            not null,

score                          dec(6,2)                       not null,

subject                        varchar(20)                    not null

);insert into tbl_user (id, name) values (1, 『beimu』);

insert into tbl_user (id, name) values (2, 『aihui』);

insert into tbl_score (id, score, subject) values (1, 90, 『語文』);

insert into tbl_score (id, score, subject) values (1, 80, 『數學』);

insert into tbl_score (id, score, subject) values (2, 86, 『數學』);

insert into tbl_score (id, score, subject) values (2, 96, 『語文』);

select a.id,sum(b.score) as sumscore

from tbl_user a left join tbl_score b

on a.id=b.id

group by a.id

mysql 如何優化left join

今天遇到乙個left join優化的問題,搞了一下午,中間查了不少資料,對mysql的查詢計畫還有查詢優化有了更進一步的了解,做乙個簡單的記錄 select c.from hotel info original c left join hotel info collection h on c.hot...

Left join 中where on 的 區別

left join中where,on區別 table a id,type id type 1 1 2 1 3 2 table b id,class id class 1 1 2 2 sql語句1 select a.b.from a left join b on a.id b.id and a.typ...

sql 中 left join 的使用

left join 是以左表為基礎,查詢右表的值。如果在右表中沒用沒有資料,則為null。這裡有三張表。線路bs line id,name id主鍵 線路段bs seg id,l id,name l id關聯線路id 配變bs dsub id,seg id,name seg id關聯線路段id 它們...