mysql 連線分析 MySQL連線查詢案例分析

2021-10-22 05:28:02 字數 1287 閱讀 1529

案例分析

例1 查出每個品種各有多少個商品

參考前幾章我的商品表

select p.protype_id,protype_name,count(*) as   數量   from    product as p inner  join     product_type as t on  p.protype_id=t.id  group by p.protype_id;

例2  湖南長沙理工學生資訊表和院系資訊表

建表create   table  tab_student(

students_id int unsigned not null auto_increment primary key comment '學生id',

student_name varchar(255) not null default '' comment '學生姓名',

*** enum('男','女') not null default '男' comment '性別',

address varchar(255) not null default '' comment '籍貫',

yuanxi_id int  not null comment '院系id'

)engine=innodb  charset=utf8;

第二張表 yuan_xi

create table yuan_xi(

yuanxi_id int unsigned not null auto_increment primary key comment '院系id',

yuanxi_name varchar(255) not null default '' comment '院系姓名',

yuanxi_dizhi varchar(255) not null default '' comment '院系位址',

yuanxi_phone    varchar(255) not null default '' comment '系辦**'

)engine=innodb  charset=utf8;

第一問題:

查出計算機系所有學生資訊

select    tab_student.*  from  tab_student inner   join  yuan_xi on  tab_student.yuanxi_id=yuan_xi.yuanxi_id where yuanxi_name='計算機系';

解析:yuanxi_name='計算機系';

周行知是計算機系的

哈妮克孜是計算機系的

林青霞是計算機系的

韓雪是計算機系的

mysql例項連線 Mysql 例項分析連線

表a記錄如下 aid anum 1 a20050111 2 a20050112 3 a20050113 4 a20050114 5 a20050115 表b記錄如下 bid bname 1 2006032401 2 2006032402 3 2006032403 4 2006032404 8 200...

Mysql連表查詢(內連線 外連線)

建立兩張 並分別插入資料 create table ifnot exists left table id int auto increment,age int,name varchar 20 primary key id engine innodb default charset utf8 auto...

08 1連線查詢 外連線 MySQL

外連線 應用場景 用於查詢乙個表中有,另乙個表中沒有的記錄 特點 1 外連線的查詢結果為主表中的所有記錄 如果從表中有和主表匹配的,則顯示匹配的值 如果從表中沒有和主表匹配的,則顯示null 外連線查詢的結果 內鏈結結果 主表中有而從表中沒有的記錄 2 左外連線 left join 左邊的是主表 右...