I'm using JDBI and would like to use the SQL Object API in order to, given a field value, search if a row exists for this value, return its key if found, create it if not found and return the generated key (talking to a MySQL database).
Is it possible to do this using SQL Object API without any stored procedure?
答案 0 :(得分:1)
JDBI SQL Object API中没有任何魔力可以帮助您。您将需要几个SQL语句,就像在存储过程中一样。
如果要将它组合在DAO类的单个方法中,可以使它成为抽象类而不是接口,那么它可以有一个包含所需逻辑的具体方法,并注释它以使其发生在交易中:
print df
a b c
0 1 a 10
1 2 a 20
2 3 a 30
3 4 b 10
4 5 b 100
df1 = df.groupby('b')['c'].mean().reset_index()
print df1
b c
0 a 20
1 b 55
print df1.c.max()
55
print df1.c.min()
20
答案 1 :(得分:1)
要正确执行此操作,您需要处理数据库中的并发。创建value
是唯一索引的表:
CREATE TABLE mytable (
id bigint NOT NULL PRIMARY KEY,
value varchar(100) NOT NULL UNIQUE
);
然后您可以使用MySQL的INSERT IGNORE
语法,该语法会跳过重复键等错误:
INSERT IGNORE INTO mytable VALUES (:value);
插入后,请阅读行:
SELECT * FROM mytable WHERE value = :value;
如果始终运行额外插入是一个性能问题,您可以先执行读取,只有在记录不存在时才插入。