Update + group concat (MySQL): verschil tussen versies

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen
Regel 91: Regel 91:
 
== Rechtstreeks ==
 
== Rechtstreeks ==
  
Complete testscript:
+
Complete testscript (<code>kbo 3.0 » 200 - Complete procedure » 040.sql</code> - 28 mei 2019):
  
 
<pre>
 
<pre>
Regel 114: Regel 114:
 
#
 
#
 
# tools_tmp
 
# tools_tmp
###################################3
+
###################################
 
#
 
#
 
# drop table if exists tools_tmp;
 
# drop table if exists tools_tmp;
 
# create table tools_tmp
 
# create table tools_tmp
 
# (
 
# (
# sku varchar(40),
+
#     sku varchar(40),   # Géén pk!
 
#    tool text
 
#    tool text
 
# );
 
# );
Regel 126: Regel 126:
  
 
# Output_tmp
 
# Output_tmp
###################################3
+
###################################
 
#
 
#
 
drop table if exists output_tmp;
 
drop table if exists output_tmp;
 
create table output_tmp
 
create table output_tmp
 
(
 
(
sku varchar(40) primary key,
+
    sku varchar(40)   primary key,   # Wél pk
 
     tools text
 
     tools text
 
);
 
);
Regel 149: Regel 149:
 
set tools =
 
set tools =
 
(
 
(
select group_concat(distinct tools_tmp.tool separator ', ')
+
    select group_concat(distinct tools_tmp.tool separator ', ')
 
     from tools_tmp
 
     from tools_tmp
 
     where tools_tmp.sku = output_tmp.sku
 
     where tools_tmp.sku = output_tmp.sku

Versie van 28 mei 2019 17:07

Hoe gebruik je group_concat icm. update? Lang gebruikte ik een tijdelijke tabel voor tussentijdse resultaten, totdat ik de oplossign vond in mei 2019:

Mbv. een tijdelijke tabel

Voorbeelden dwh:

  • sq_prepare_brands_per_sku_combined
  • sq_prepare_orgbrush_combined.

sq_prepare_brands_per_sku_combined:

CREATE DEFINER=`root`@`localhost` PROCEDURE `sq_prepare_brands_per_sku_combined`()
BEGIN

# Create table "brands_per_sku_tmp"
#############################################################################################
#
# * Main input: Table "sq_root_tmp" & "brush_tool_org_201808"

#############################################################################################
# Initialise table "brands_per_sku_tmp" with "sku"
#############################################################################################
#
drop table if exists brands_per_sku_tmp;

create table brands_per_sku_tmp
(
    sku		varchar(40)	primary key,
    sku_oem	varchar(40),
    brands	text
);

insert into brands_per_sku_tmp (sku, sku_oem)
select sku, sku_oem from root_tmp;


#############################################################################################
#  Select brands per sku → Temporary table "brands_per_sku_tmp2"
#############################################################################################
#
# Reason for using this temporary temporary table: I don't know how to change the code below
# to update an existing table, rather than creating a new one + don't want to spend too much
# time on it (May 2019)
#
# Test
#####################
#
# select distinct
#
#    root_tmp.sku,
#    root_tmp.sku_oem,
#    group_concat(distinct tool_brand separator ', ') as brands
#
# from root_tmp
# join brush_tool_org_201808 on root_tmp.sku_oem = brush_tool_org_201808.sku
#
# group by root_tmp.sku;

# Real
#####################
#
drop table if exists brands_per_sku_tmp2;

create table brands_per_sku_tmp2

select distinct

    root_tmp.sku,
    # root_tmp.sku_oem,
    group_concat(distinct tool_brand separator ', ') as brands

from root_tmp
join brush_tool_org_201808 on root_tmp.sku_oem = brush_tool_org_201808.sku

group by root_tmp.sku;


#############################################################################################
#  Update "brands_per_sku_tmp" with "brands_per_sku_tmp2"
#############################################################################################
#
update brands_per_sku_tmp
join brands_per_sku_tmp2 on brands_per_sku_tmp.sku = brands_per_sku_tmp2.sku
set
	brands_per_sku_tmp.brands	=	brands_per_sku_tmp2.brands;

END

Rechtstreeks

Complete testscript (kbo 3.0 » 200 - Complete procedure » 040.sql - 28 mei 2019):

#
# KBO 3.0: Figure out group_concat + update
#####################################################################################################################
#
#####################################################################################################################
# Casus
#####################################################################################################################
#
# * Tabel root_tmp: Kolom "sku"
# * Tabel tool_tmp: Kolom "sku" & kolom "tool"
#
# * Tabel "output_tmp" met kolommen "sku" & "tools" met daarin alle tools (comma+spatie-separated)
#
use dwh;

#####################################################################################################################
# Initialise test tables
#####################################################################################################################
#
# tools_tmp
###################################
#
# drop table if exists tools_tmp;
# create table tools_tmp
# (
#     sku	varchar(40),   # Géén pk!
#     tool	text
# );

#insert into tools_tmp select sku, tool_main_id from brush_tool_org_201808;

# Output_tmp
###################################
#
drop table if exists output_tmp;
create table output_tmp
(
    sku		varchar(40)   primary key,   # Wél pk
    tools	text
);

# This is the trick: I want ot use update, as the output table is already initialised
#####################################################################################
#
insert into output_tmp (sku)
select sku from root_tmp;


#####################################################################################################################
# Doen!
#####################################################################################################################
#
update output_tmp
set tools =
(
    select group_concat(distinct tools_tmp.tool separator ', ')
    from tools_tmp
    where tools_tmp.sku = output_tmp.sku
    group by output_tmp.sku
);


#####################################################################################################################
# Debug
#####################################################################################################################
#
select * from tools_tmp;
select * from output_tmp;

Bronnen