筆記:實現內連線
以運算元據庫tb_train_cpp2和tb_train_cpp22為例:
desc tb_train_cpp2;
desc tb_train_cpp22;
資料表tb_train_cpp2中的字段country與tb_train_cpp22中的字段cname對應[可實現內連線]
select tb_train_cpp2.username,tb_train_cpp2.age,tb_train_cpp2.position,tb_train_cpp22.people_num
from tb_train_cpp2
join tb_train_cpp22 on tb_train_cpp2.country=tb_train_cpp22.cname;
以運算元據表tb_train_user和tb_train_province為例:
desc tb_train_user;
desc tb_train_province;
select u.userid,u.username,u.score,p.proname
from tb_train_user as u
join tb_train_province as p on u.proid=p.id;
建立資料表tb_train_cpp31和tb_train_cpp32和tb_train_cpp33:
create table if not exists tb_train_cpp3(
userid tinyint unsigned key auto_increment,
username varchar(10) not null unique,
usercard char(5) not null,
proname varchar(10) default '四川-成都'
)engine=innodb charset=utf8;
insert tb_train_cpp3(username,usercard,proname) values
('呂布','10058','河南-信陽'),
('張頜','17744','四川-綿陽'),
('曹操','11684','遼寧-鞍山'),
('趙子龍','10892','江蘇-南京'),
('貂蟬','10906','湖北-武漢'),
('甘寧','12480',default),
('張遼','18745','安徽-合肥'),
('夏侯惇','19003','河南-開封'),
('左慈','19869','廣東-廣州'),
('趙飛燕','16891','湖南-岳陽'),
('張飛','12565','陝西-西安'),
('袁紹','12007','新疆-烏魯木齊');
create table if not exists tb_train_cpp33(
proname varchar(10) not null,
proid tinyint unsigned
insert tb_train_cpp33 values
('河南-信陽',12),
('四川-綿陽',25),
('遼寧-鞍山',38),
('江蘇-南京',44),
('湖北-武漢',50),
('四川-成都',28),
('安徽-合肥',69),
('河南-開封',17),
('廣東-廣州',72),
('湖南-岳陽',86),
('陝西-西安',99),
('新疆-烏魯木齊',105);
create table tb_train_cpp33(
username varchar(10) not null unique,
age int unsigned default 18,
*** enum('男','女','保密'),
ablity float(4,2)
insert tb_train_cpp33 values
('呂布',36,'男',78.24),
('張頜',32,'女',75.87),
('曹操',45,'男',94.66),
('趙子龍',default,'保密',90.18),
('貂蟬',24,'女',82.51),
('甘寧',28,'男',85.90),
('張遼',40,'男',89.02),
('夏侯惇',37,'保密',76.34),
('左慈',56,'女',91.48),
('趙飛燕',20,'女',88.63),
('張飛',48,'男',64.99),
('袁紹',50,'保密',76.89);
select t1.username,t1.usercard,t2.proid
from tb_train_cpp31 as t1
join tb_train_cpp32 as t2 on t1.proname=t2.proname;
select t1.username,t1.usercard,t3.age,t3.***,t3.ablity,t1.proname
from tb_train_cpp31 as t1
join tb_train_cpp33 as t3 on t1.username=t3.username;
MySQL內連線排序
student1 學生表 grade1 年級表 result 考試成績表 subject 科目表 都在上乙個部落格 查詢參加考試的同學資訊 學號 姓名 科目號 成績 select s.studentno,studentname,subjectno,studentresult from student...
mysql 內連線查詢
例7.46 在fruits表和suppliers表之間使用內連線查詢,查詢之前,檢視兩個表的結構,select suppliers.s id,s name,f name,f price from fruits suppliers where fruits.s id suppliers.s id 例7...
mysql左右連線舉例 mysql左連線內連線閒談
最近忙著開發x省冷鏈追溯系統,天天乾到晚上十一點多才回到家,週末也加班,沒啥時間寫部落格,閒下來再好好寫寫 業務 sql語句統計出入庫資料。問題 只統計了x端入庫單。原因 沒有發現x端的資料庫中只有入庫單是有a欄位資料而出庫單是沒有的,並使用了a欄位去inner join,導致只能查到入庫單進行統計...