{"id":1197,"date":"2023-10-07T11:33:03","date_gmt":"2023-10-07T03:33:03","guid":{"rendered":"https:\/\/hisui.cn\/?p=1197"},"modified":"2025-09-08T17:06:44","modified_gmt":"2025-09-08T09:06:44","slug":"hos%e5%bc%80%e5%8f%91%e7%95%8c%e9%9d%a2","status":"publish","type":"post","link":"http:\/\/hisui.cn\/?p=1197","title":{"rendered":"\u4fe1\u521b\u5f00\u53d1\u4e4bSQL\u76f8\u5173\u8bb0\u5f55"},"content":{"rendered":"<hr \/>\n<h2>1. \u5efa\u8868<\/h2>\n<pre><code class=\"language-sql line-numbers\">-- ho_his.cf_bsp_addins definition\n-- \u521b\u5efaID\u8ba1\u6570\u5668\u7cfb\u5217\uff0cid SERIAL\u65f6\uff0c\u81ea\u52a8\u751f\u6210\u7cfb\u5217\n-- create sequence \"ho_his\".cf_bsp_addins_id_seq increment by 1 start with 1\n-- \u521b\u5efa\u8868\n-- create_datetime,update_datetime\u4e3a\u7ea6\u5b9a\u6bcf\u5f20\u8868\u90fd\u9700\u8981\u7684\u5b57\u6bb5\n\nCREATE TABLE ho_his.cf_bsp_addins (\n    id SERIAL PRIMARY KEY, -- PostgreSQL,\u4eba\u5927\u91d1\u4ed3\n    id INT AUTO_INCREMENT PRIMARY KEY, -- TDSQL\n    -- id numeric(10,0) DEFAULT nextval('ho_his.cf_bsp_addins_id_seq'::regclass) NOT NULL, \/\/ \u6570\u636e\u5e93\u81ea\u52a8\u751f\u6210\n    code varchar(100) DEFAULT NULL::varchar NOT NULL,\n    domId varchar(100) DEFAULT NULL::varchar NOT NULL,\n    create_datetime timestamp NOT NULL,\n    update_datetime timestamp NOT NULL,\n    create_user_dr varchar(32) NOT NULL,\n    update_user_dr varchar(32) NOT NULL,\n    activity bool DEFAULT true NOT NULL,\n    CONSTRAINT cf_bsp_addins_pkey PRIMARY KEY (id)\n);\nCREATE UNIQUE INDEX cf_bsp_addins_domid_un ON ho_his.cf_bsp_addins (\"domId\");\nCREATE UNIQUE INDEX cf_bsp_addins_un ON ho_his.cf_bsp_addins (code);\nCOMMENT ON COLUMN \"ho_his\".\"cf_bsp_addins\".\"id\" IS '\u4e3b\u952e';\nCOMMENT ON COLUMN \"ho_his\".\"cf_bsp_addins\".\"code\" IS '\u4ee3\u7801';\nCOMMENT ON COLUMN \"ho_his\".\"cf_bsp_addins\".\"domId\" IS '\u8c03\u7528id';\n<\/code><\/pre>\n<h2>2. \u8868\u8ba1\u6570\u5668\uff08\u4eba\u5927\u91d1\u4ed3\u6d4b\u8bd5\uff09<\/h2>\n<pre><code class=\"language-sql line-numbers\">-- \u67e5\u8be2seq - \u81ea\u589e\u52a0\u53d8\u91cf\nSELECT * FROM sys_statio_all_sequences WHERE relname LIKE '%bsp_msg%'\n\n-- \u8ba9\u8ba1\u6570\u5668\u5f520\n-- \u8bbe\u7f6e\u6b65\u8c03-100\nALTER SEQUENCE cf_bsp_msg_x_id_seq INCREMENT BY -100 ;\n-- \u8c03\u7528\u4e00\u6b21seq, -100\nSELECT nextval('cf_bsp_msg_x_id_seq'::regclass);\n-- \u8c03\u7528\u4e00\u6b21seq,-100\nSELECT nextval('cf_bsp_msg_x_id_seq'::regclass) ;\n<\/code><\/pre>\n<h2>3.\u67e5\u8be2\u6761\u4ef6<\/h2>\n<p>\u5728mybatis.xml\u4e2d\u5ffd\u7565\u7279\u6b8a\u5b57\u7b26\u67e5\u8be2\u8bb0\u5f55\u5199\u6cd5<\/p>\n<pre><code class=\"language-sql line-numbers\">select code, id from cf_bsp_alert_code t\nwhere REGEXP_REPLACE(t.code,'[^A-Za-z0-9]','') like concat('%',REGEXP_REPLACE(#{dto.code},'[^A-Za-z0-9]',''),'%')\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>ilike<\/code>\u6765\u6a21\u7cca\u67e5\u8be2<\/p>\n<pre><code class=\"language-sql line-numbers\">(code ilike concat('%', #{dto.code}, '%') or description ilike concat('%',#{dto.code},'%'))\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>UPPER<\/code>\u8f6c\u5927\u5199\uff0c\u518d\u6a21\u7cca\u67e5\u8be2<\/p>\n<pre><code class=\"language-sql line-numbers\">(UPPER(code) like concat('%', UPPER(#{dto.code}), '%') or UPPER(description) like concat('%',UPPER(#{dto.code}),'%'))\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>CURRENT_DATE<\/code>\u6765\u5224\u65ad\u5f53\u524d\u662f\u4e0d\u662f\u6709\u6548<\/p>\n<pre><code class=\"language-sql line-numbers\">AND d.activity=true AND d.is_deleted=0\nAND (t.start_date is NULL OR CURRENT_DATE&gt;=t.start_date)\nAND (t.end_date IS NULL OR t.end_date&gt;=CURRENT_DATE)\nORDER BY d.id ASC\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>TO_DATE<\/code>\u6765\u67e5\u8be2\u8868\u4e2d\u6570\u636e<\/p>\n<pre><code class=\"language-sql line-numbers\">AND d.activity=true AND d.is_deleted=0\nAND (t.start_date is NULL OR to_date('20250501','yyyymmdd')&gt;=t.start_date)\nAND (t.end_date IS NULL OR t.end_date&gt;=CURRENT_DATE)\nORDER BY d.id ASC\n<\/code><\/pre>\n<h2>4.\u8054\u8868\u67e5\u8be2\u65f6\uff0c\u8054\u8868\u5b57\u6bb5\u7c7b\u578b\u4fdd\u6301\u4e00\u81f4,\u624d\u4f1a\u4f7f\u7528\u7d22\u5f15<\/h2>\n<p><code>cf_bsp_test_patient<\/code>\u8868\u4e2d\u53ea\u67093\u884c\u8bb0\u5f55,pa_pat_mast\u8868\u670928\u4e07\u884c\u6570\u636e<\/p>\n<pre><code class=\"language-sql line-numbers\">explain analyze\nselect t.id,t.patient_id,t.start_date,t.end_date, p.name \"patientName\"\n    from cf_bsp_test_patient t\n    left join pa_pat_mast p on p.id=t.patient_id\n    where 1=1 and (t.start_date &gt;= '20250501' or t.start_date is null) order by t.id asc LIMIT 500\n<\/code><\/pre>\n<p>\u4e0a\u9762\u89e3\u6790sql\uff0c\u8bc4\u4f30\u8fd0\u884c\u82b1\u8d39350ms\u65f6\u95f4<\/p>\n<pre><code class=\"language-sql line-numbers\">explain analyze\nselect t.id,t.patient_id,t.start_date,t.end_date, p.name \"patientName\"\n    from cf_bsp_test_patient t\n    left join pa_pat_mast p on p.id=cast(t.patient_id as bigint)\n    where 1=1 and (t.start_date &gt;= '20250501' or t.start_date is null) order by t.id asc LIMIT 500\n<\/code><\/pre>\n<p>\u8fde\u8868\u524d\u8f6c\u6362\u5b57\u6bb5\u7c7b\u578b\uff0c\u6b64\u65f6\u8fd0\u884cSQL,\u89e3\u6790SQL,\u8bc4\u4f30\u8fd0\u884c\u82b1\u8d390.129ms<\/p>\n<p>\u4fee\u6539cf_bsp_test_patient\u8868\u7684patient_id\u5b57\u6bb5\u7c7b\u578b\u4fee\u6539\u6210bigint\u540e\uff0c\u901f\u5ea6\u63d0\u5347\u3002<\/p>\n<h2>5. \u63d2\u5165\u5173\u8054\u8868<\/h2>\n<p>\u5c1d\u8bd5\u4f7f\u7528returning\u8fd4\u56de\u63d2\u5165id\u5e76\u4e0d\u80fd\u5b9e\u73b0\uff0c\u6700\u7ec8\u4f7f\u7528\u518d\u6b21\u67e5\u8be2<\/p>\n<pre><code class=\"language-sql line-numbers\">INSERT INTO cf_bsp_prdline\n(code, caption, note, create_datetime, update_datetime, hospital_dr, create_user_dr, update_user_dr, activity, order_no)\nVALUES ('x', '\u63cf\u8ff0', '', now(), now(), -1, '0', '0', true, 14) ;\n\nINSERT INTO cf_bsp_prdline_module\n(code,prdline_dr, caption, note, create_datetime, update_datetime, hospital_dr, \n create_user_dr, update_user_dr, activity, order_no)\nSELECT m.code,new_prdline.id,m.caption,m.note,m.create_datetime,m.update_datetime,m.hospital_dr,\nm.create_user_dr,m.update_user_dr,m.activity,m.order_no\nFROM (select id from cf_bsp_prdline where code='doctortt') new_prdline,\n     ( VALUES\n           ('xx1', '\u63cf\u8ff01', '', now(), now(), NULL, '0', '0', true, NULL),\n           ('xx2', '\u63cf\u8ff02', '', now(), now(), NULL, '0', '0', true, NULL),\n           ('xx3', '\u63cf\u8ff03', '', now(), now(), NULL, '0', '0', true, NULL),\n           ('xx4', '\u63cf\u8ff04', '', now(), now(), NULL, '0', '0', true, NULL),\n           ('xx5', '\u63cf\u8ff05', '', now(), now(), NULL, '0', '0', true, NULL)\n     ) AS m(code, caption, note, create_datetime, update_datetime, hospital_dr,\n            create_user_dr, update_user_dr, activity, order_no);\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>HOS\u5f00\u53d1\u754c\u9762\u6b65\u9aa4\u6f14\u793a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[200,190,199,225,237],"class_list":["post-1197","post","type-post","status-publish","format-standard","hentry","category-imedical_info","tag-create-table","tag-his","tag-id-seq","tag-sql","tag-237"],"_links":{"self":[{"href":"http:\/\/hisui.cn\/index.php?rest_route=\/wp\/v2\/posts\/1197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/hisui.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/hisui.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/hisui.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/hisui.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1197"}],"version-history":[{"count":0,"href":"http:\/\/hisui.cn\/index.php?rest_route=\/wp\/v2\/posts\/1197\/revisions"}],"wp:attachment":[{"href":"http:\/\/hisui.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/hisui.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1197"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/hisui.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}