This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-06-02
Channels
- # ai (1)
- # aleph (16)
- # announcements (1)
- # architecture (51)
- # babashka (32)
- # beginners (27)
- # calva (3)
- # clerk (1)
- # clojure (49)
- # clojure-art (1)
- # clojure-denver (6)
- # clojure-europe (70)
- # clojure-nl (1)
- # clojure-norway (56)
- # clojure-uk (2)
- # clojuredesign-podcast (4)
- # clojurescript (57)
- # clr (15)
- # community-development (3)
- # conjure (1)
- # core-async (10)
- # data-science (1)
- # datalog (2)
- # datomic (3)
- # emacs (12)
- # events (1)
- # gratitude (4)
- # honeysql (9)
- # hyperfiddle (86)
- # jobs (4)
- # off-topic (10)
- # pedestal (5)
- # portal (11)
- # practicalli (2)
- # reitit (7)
- # releases (3)
- # remote-jobs (1)
- # sql (15)
- # tools-build (8)
- # xtdb (4)
attempting to create a multi-insert-on-duplicate-update mysql statement in the form of
INSERT INTO beautiful (name, age)
VALUES
('Helen', 24),
('Katrina', 21),
('Samia', 22),
('Hui Ling', 25),
('Yumie', 29)
AS newvals
ON DUPLICATE KEY UPDATE
age = newvals.age
...
and am trying to understand what I might need to get that newvals
values "alias" to generate
I feel like I'm close, this is what I've got so far:
(def csvrows '(["id" "c1" "c2"] [1 "two" "four"] [2 "five" "six"]))
(let [cols (first csvrows)]
(-> (insert-into :mytable)
(with-columns cols)
(values (rest csvrows))
(on-duplicate-key-update (zipmap cols (map (fn [x] (str "newvals." x)) cols)))
(sql/format {:dialect :mysql}))))
Which produces
["INSERT INTO `mytable` (?, ?, ?)
VALUES (?, ?, ?), (?, ?, ?)
ON DUPLICATE KEY UPDATE
`id` = ?, `c1` = ?, `c2` = ?"
"id" "c1" "c2"
1 "two" "four"
2 "five" "six"
"newvals.id" "newvals.c1" "newvals.c2"]
Use keywords instead of strings for identifiers such as table, column, or function names.
For qualified identifiers, use either a dot directly or /
, so either :newvals.age
or :newvals/age
. Nowadays, I myself prefer the latter.
And if you need to construct such keywords at run time, it's easy to do with (keyword "newvals" "age")
.
@U0482NW9KL1 did that answer your question? I'm not sure how you can create an alias in that position, mainly because I'm not sure which part of the statement the alias belongs to - I've never seen it in that position before.
apologies, i was left without computer access soon after tossing out that question :^)
and that was a stack-overflow recommended solution for the case where one might want to make an INSERT+ON DUPLICATE KEY UPDATE
on multiple inserts/rows at once, where in the case of a duplicate key, I'd want all values for that row to be updated to the new ones
SO post for reference: https://stackoverflow.com/questions/2714587/mysql-on-duplicate-key-update-for-multiple-rows-insert-in-single-query
i haven't gotten the chance to test himik's solution yet, but I don't think its exactly what im looking for
OK, keep us posted...