mysql - 是否有触发器可以帮助我从日常记录表中更新报告表?我
问题描述
我有两个表,如下所示: Table_Report需要在以下条件下从Daily_Records更新:
需要通过连接所有 PIN 相同的Sold_Item来更新Sold_Summary列。不包括需要在Special_sold列中记录的特价商品 {笔记本电脑、智能手机、平板电脑}。
当 PIN 相同时,需要通过连接所有Dest来更新Dest_Summary列。
当 PIN 相同时, Special_sold列需要以相同的方式更新。
Special_sold列只在Sold_Item列中查找,如果它发现任何特殊的已售 {笔记本电脑,智能手机,平板电脑},当 PIN 相同时,它会在Special_sold下记录。
表 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 | 桌子 | 哈拉雷 | 无效的 |
解决方案
我不会为此使用触发器。只需运行查询:
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
推荐阅读
- android - 膨胀类 ImageButton 时出错(Android KitKat)
- r - 如何用R中同一行中的前一个值替换数据框中的任何NA
- docker - 将 Elasticsearch 日志迁移到不同的集群
- reactjs - 功能组件中渲染之间的数据持久性
- sql-server - SSMS“此版本的 SQL Server 不支持 Windows 登录”
- awk - 将字符串与多个数组连接
- python - 如何在我正在编写的许多大型项目中跟踪和重用我编写的小项目?
- java - 我想创建向量动态的对象取决于任何 excel 文件(Jtable)中的数字列
- c - AddressSanitizer:C 中链表的 DEADLYSIGNAL 错误
- ios - xcrun:在 Mac OS 上安装 Regex 时出错