首页 > 解决方案 > 是否有触发器可以帮助我从日常记录表中更新报告表?我

问题描述

我有两个表,如下所示: Table_Report需要在以下条件下从Daily_Records更新:

表 1:Daily_Records

**ID 日期 别针 已售商品 目的地**
1 2021-04-20 110 达瓦
2 2021-04-21 131 桌子 哈拉雷
3 2021-04-22 110 椅子 贡达尔
4 2021-04-23 120 手机 达瓦
5 2021-04-24 111 笔记本电脑 阿达玛
6 2021-04-25 120 椅子 达瓦
7 2021-04-26 111 哈拉雷

表2: Table_Report

**ID 别针 Sold_Summary Dest_Summary 特价_已售**
1 110 包,椅子 达瓦,贡达尔 无效的
2 111 阿达玛,哈拉雷 笔记本电脑
3 120 椅子 达瓦 手机
4 131 桌子 哈拉雷 无效的

标签: mysqlsqldatabasetriggers

解决方案


我不会为此使用触发器。只需运行查询:

select pin,
       group_concat(distinct case when sold_item not in ('Laptop', 'Smartphone', 'Tablet') then sold_item end) as sold_items,
       group_concat(distinct case when sold_item in ('Laptop', 'Smartphone', 'Tablet') then sold_item end) as sold_items_special,
       group_concat(distinct dest) as dest
from daily_records
group by pin;

你不解释是什么id。您可以添加这样的列:

row_number() over (order by pin) as id

推荐阅读