I have an SQL script, which returns the data from a Clickhouse table in amount of ~23k rows. I want to insert that exact data into a table in MySQL. Since they’re two different DBs and there’s no proxy to use (so no INSERT INTO SELECT
), seems that the only option i’m left with is to make an insert script with the exact data i have so i can copy-past it and run on another DB.
In another words, let’s say i have a script
SELECT date, name, sumIf(events>0) ev
FROM t1
GROUP BY date, name
which returns the data
2023-10-31 'George' 1261169
2023-11-01 'Alan' 1261370
2023-11-02 'Peter' 1261361
How can i modify my script so it returns something like
INSERT INTO t2 VALUES (2023-10-31, 'George', 1261169), (2023-11-01, 'Alan', 1261370), (2023-11-02, 'Peter', 1261361);
?
2
Answers
here is the code that generates needed insert statement
Output:
You may use clickhouse playground to test things:
https://play.clickhouse.com/play?user=play#c2VsZWN0IDE=
Unfortunately there is not arrayFold function in playground for some reason, this function may simplify code, but I made an ad-hoc solution with string manipulations
There is a more elegant method for this.
The output will be something like this:
Bonus
You can create a link between your ClickHouse and MySQL instance and directly insert data into MySQL using MySQL table engine: