mysql 内连接、左连接、右连接

SQL表操作与连接查询
本文介绍了如何使用SQL创建及删除表,并通过示例展示了不同类型的表连接查询,包括内连接、左连接和右连接的具体用法。

记录备忘下,初始数据如下:

DROP TABLE IF EXISTS t_demo_product;
CREATE TABLE IF NOT EXISTS t_demo_product(
    proid int(20),
    proname varchar(20),
    price int(10),
    PRIMARY KEY(proid)
)ENGINE=InnoDB  DEFAULT CHARSET=gbk;

DROP TABLE IF EXISTS t_demo_operation;
CREATE TABLE IF NOT EXISTS t_demo_operation(
    opid  int(10),
    proid int(20),
    opcount int(10),
    PRIMARY KEY(opid)
)ENGINE=InnoDB  DEFAULT CHARSET=gbk;


insert into t_demo_product(proid, proname, price) values
(1,'产品A','10')
,(2,'产品B','11')
,(3,'产品C','12')
,(4,'产品D','13')
,(5,'产品E','14');

insert into t_demo_operation(opid, proid, opcount) values
(1,1,2)
,(2,3,6)
,(3,3,1)
,(4,4,3)
,(5,7,3);

//内连接,不允许未空
select * from t_demo_product a, t_demo_operation b where a.proid = b.proid;
select * from t_demo_product a join t_demo_operation b on a.proid = b.proid;

//左连接,以t_demo_product为主,若对应存在多条记录则记录多条,无则为空
select * from t_demo_product a left join t_demo_operation b on a.proid = b.proid;

//右连接, 以t_demo_operation为主,若对应多条则记录多条,无则为空
select * from t_demo_product a right join t_demo_operation b on a.proid = b.proid;

转载于:https://www.cnblogs.com/Fredric-2013/p/5082613.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值