ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

MySQL left join 引发的惨案

2022-04-21 16:03:11  阅读:152  来源: 互联网

标签:join artcle blog MySQL expense cateid id left


当我用这个进行更改值时,type未控制order表 其他数据被更改 还好备份数据表了(这里就体现了备份的重要性)

 UPDATE expense_order as a
    left join (
        SELECT detail.company_id,detail.`order_id`,sum(detail.`deduction_money`) as amount FROM expense_amortize_detail as detail

JOIN  `pigcms_expense_order` as expense on expense.`id` = detail.`order_id` 

WHERE detail.`company_id` =336 and detail.`status` = 0 

and expense.`company_id` =336 and expense.type=2 and expense.is_parent = 1 and  expense.status in (1,2) and expense.auditing_status = 1

GROUP BY(expense.`id`)  ORDER BY expense.`id` DESC
				) as d
    on a.id =d.order_id and a.company_id = d.company_id  and a.type =2
	set a.settlement_money=d.amount; 

还原表数据 同样left join 不同之处在于通过 where控制左联表的判断依据

 UPDATE expense_order as a
    left join expense_order_bak as bak 
    on a.id =bak.id
	set a.settlement_money=bak.settlement_money 
	where a.type=1

其他解决方案 将left join 更改为inner join

 UPDATE expense_order as a
    inner join (
        SELECT detail.company_id,detail.`order_id`,sum(detail.`deduction_money`) as amount FROM expense_amortize_detail as detail

JOIN  `pigcms_expense_order` as expense on expense.`id` = detail.`order_id` 

WHERE detail.`company_id` =336 and detail.`status` = 0 

and expense.`company_id` =336 and expense.type=2 and expense.is_parent = 1 and  expense.status in (1,2) and expense.auditing_status = 1

GROUP BY(expense.`id`)  ORDER BY expense.`id` DESC
				) as d
    on a.id =d.order_id and a.company_id = d.company_id  and a.type =2
	set a.settlement_money=d.amount; 

案例

localhost 新建 blog_cate 栏目表 blog_artcle 文章表

-- 删除blog库
DROP DATABASE IF EXISTS blog;
-- 新建blog库
CREATE DATABASE blog charset utf8;
-- 查看、进入
show database;
use blog;

-- 先删后建栏目表
drop table if exists blog_cate ;
create table blog_cate (
	id INTEGER,
	catename varchar(30)
);
-- 同理 建文章表blog_artcle
--- 插入数据
INSERT INTO `blog_cate `(`id`, `catename `) VALUES (1, '父亲的散文诗');
INSERT INTO `blog_cate `(`id`, `catename `) VALUES (2, '心情文字');

INSERT INTO `blog_artcle`(`id`, `title`, `desc`, `content`, `cateid`, `time`, `pic`) VALUES (1, 'test', '测试测试', '父亲的散文诗', 1, 2022, 'dd');
INSERT INTO `blog_artcle`(`id`, `title`, `desc`, `content`, `cateid`, `time`, `pic`) VALUES (2, 'test1', '测试测试1', '父亲的散文诗1', 1, 2022, 'dd');
INSERT INTO `blog_artcle`(`id`, `title`, `desc`, `content`, `cateid`, `time`, `pic`) VALUES (4, 'test4', '测试测试4', '父亲的sf散文诗1', 2, 2022, 'dd');
INSERT INTO `blog_artcle`(`id`, `title`, `desc`, `content`, `cateid`, `time`, `pic`) VALUES (3, 'test3', '测试测试3', 'dfsfsfs', 2, 2022, 'dd');

blog_cate

blog_cate

blog_artcle

blog_artcle

数据库连表方式

  • 内连接 :inner 、inner join
  • 外连接 :outer join
    • 左外连接 :left outer join
    • 左连接 :left join
    • 右外连接 right outer join
    • 右连接: right join
  • 全连接 full join 、union

内连接

查询的是两张表的并集,也就是A表和B表都必须有数据才能查询出来;

/*** 栏目的id 与 文章的所属栏目id */
-- join
select * from blog_cate as c join blog_artcle as a  on c.id = a.cateid 

-- inner join
select * from  blog_cate as c  inner join blog_artcle as a  on c.id = a.cateid 

-- 逗号的连表方式就是内连接
select * from blog_cate as c, blog_artcle as a where c.id = a.cateid

/**栏目的id 与 文章的id 进行查询*/
select * from  blog_cate as c  inner join blog_artcle as a  on c.id = a.id 

结果展示

结果展示
结果展示

左外连接 和 左连接

是以左表为基础,根据ON后给出的两表的条件将两表连接起来。结果会将左表所有的查询信息列出,而右表只列出ON后条件与左表满足的部分。左连接全称为左外连接,是外连接的一种。

/*** 栏目的id 与 文章的id */
-- left join
select * from blog_cate as c left join  blog_artcle as a on  c.id = a.id

-- left outer join
select * from blog_artcle as c left outer join blog_cate as a on   c.id = a.id 

-- left join

left join

-- left outer join 两个表更换位置

left outer join

右外连接 和 右连接

是以右表为基础,根据ON后给出的两表的条件将两表连接起来。结果会将右表所有的查询信息列出,而左表只列出ON后条件与右表满足的部分。右连接全称为右外连接,是外连接的一种。

-- right join
select * from blog_cate as c right join  blog_artcle as a on  c.id = a.id

-- right outer join
select * from blog_artcle as c right outer join blog_cate as a on   a.id = c.id 

与上面左联接进行比较

-- right join 两个表更换位置

-- right join

-- right outer join

right outer join

全连接

全连接显示两侧表中所有满足检索条件的行。
mysql中没有full join,mysql可以使用union实现全连接;

select * from blog_cate as c left join blog_artcle as a on a.id = c.id 
union
select * from blog_cate as c  right join blog_artcle as a on c.id = a.cateid 

全连接

实验 今日出现的问题

步骤1 定目标

-- 修改文章cateid 为2的内容 为所属栏目的name
UPDATE blog_artcle  as a 
LEFT JOIN blog_cate as c
on a.cateid = c.id and a.cateid = 2
SET a.content = c.catename

步骤2 首先看下原表内容

SELECT * FROM blog_artcle
原表内容

步骤3 执行 查看差异

查看差异

有2行被修改,但是cateid=1的直接被修改到为null 可知and a.cateid = 2 这个 条件我们没有控制到 cateid=1的数据已被更改为null

查看差异

步骤4 更改SQL语句

--- left join 与 where 结合 改cateid =2 的
UPDATE blog_artcle  as a 
LEFT JOIN blog_cate as c
on c.id = a.cateid 
SET a.content = c.catename WHERE a.cateid = 2
--- inner join 与on 结合 改cateid =1 的
UPDATE blog_artcle  as a 
inner JOIN blog_cate as c
on a.cateid = c.id and a.cateid =1
SET a.content = c.catename

成功

更改SQL语句

右连接可参考左联接 这里就完结了 今天圆满一天

标签:join,artcle,blog,MySQL,expense,cateid,id,left
来源: https://www.cnblogs.com/depressiom/p/16174481.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有