函数定义
--药物名称提取
CREATE OR REPLACE FUNCTION "pygetdrugname"("text" text)
RETURNS "text" AS $BODY$
global text
from sqlalchemy import create_engine
import pandas as pd
name='root'#mysql用户名
pw=''#mysql密码
host='127.0.0.1'#mysql连接地址
port='3306'
database='dev'#数据库
# 用sqlalchemy构建数据库链接engine
connect_info = 'mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8'.format(name,pw,host,port,database)
database_info = create_engine(connect_info)
data=pd.read_sql('SELECT DISTINCT me_name1 FROM be_bianma WHERE LENGTH(me_name1)>1 ORDER BY LENGTH(me_name1) DESC;',database_info)
drug=[]
for i in data['me_name1'].tolist():
if i in text:
drug.append(str(i))
ret='/'.join(drug)
if len(ret)==0:
ret=None
return ret
$BODY$
LANGUAGE plpython3u;
函数使用
-- 药品名称提取
SELECT
me_title,
pygetdrugname(me_title)
FROM
be_linchuangshiyan
WHERE
laiyuan = '中国临床试验注册中心'
AND
pygetdrugname(me_title) IS NOT NULL;
处理结果案例