{"id":9500,"date":"2025-11-11T17:58:28","date_gmt":"2025-11-11T09:58:28","guid":{"rendered":"\/?p=9500"},"modified":"2025-11-11T17:58:28","modified_gmt":"2025-11-11T09:58:28","slug":"%e6%8f%90%e7%a4%ba%e8%af%8d%e6%80%8e%e4%b9%88%e5%86%99%ef%bc%8c%e4%bb%a5-text2sql-%e4%b8%ba%e4%be%8b","status":"publish","type":"post","link":"\/?p=9500","title":{"rendered":"\u63d0\u793a\u8bcd\u600e\u4e48\u5199\uff0c\u4ee5 Text2SQL \u4e3a\u4f8b"},"content":{"rendered":"<p><strong>\u63d0\u793a\u8bcd\u662f\u4eba\u4e0e\u5927\u6a21\u578b\u4e4b\u95f4\u6c9f\u901a\u7684\u8bed\u8a00<\/strong>\uff0c\u5176\u91cd\u8981\u6027\u4e0d\u8a00\u800c\u55bb\uff0c\u5199\u4e86\u5f88\u591a\u63d0\u793a\u8bcd\u4e5f\u8e29\u4e86\u4e0d\u5c11\u5751\uff0c\u8ddf\u5927\u5bb6\u5206\u4eab\u4e00\u4e9b\u5fc3\u5f97\uff0c\u672c\u6587\u4ee5 Text2SQL \u4e3a\u4f8b\uff0c\u8be6\u7ec6\u62c6\u89e3\uff0c\u9010\u4e00\u5206\u6790\u3002<\/p>\n<blockquote>\n<p>\u6ce8\uff1a\u672c\u6587\u5173\u6ce8 [\u8bed\u8a00\u6a21\u578b] \u7684\u63d0\u793a\u8bcd\uff0c[\u89c6\u89c9\u6a21\u578b] \u63d0\u793a\u8bcd\u5ef6\u4f38\u9605\u8bfb\u8bf7\u770b\u6587\u672b\u94fe\u63a5~<\/p>\n<\/blockquote>\n<h3>\u63d0\u793a\u8bcd\u7684\u5de5\u7a0b\u54f2\u5b66\uff1a<strong>\u601d\u8003\u548c\u8868\u8fbe\u4f60\u60f3\u8981\u4ec0\u4e48<\/strong> - \u6e05\u6670\u3001\u51c6\u786e\u3001\u7cbe\u70bc<\/h3>\n<h3>\u63d0\u793a\u8bcd\u7684\u6280\u672f\u76f4\u89c9\uff1a\u901a\u8fc7\u63d0\u793a\u8bcd<strong>\u6fc0\u6d3b\u5927\u6a21\u578b\u7684\u76f8\u5173\u53c2\u6570<\/strong><\/h3>\n<p>tips \u5177\u4f53\u6765\u8bf4\uff1a<\/p>\n<ul>\n<li>\n<p>\u7cbe\u7b80\uff0c\u62d2\u7edd\u5e9f\u8bdd<\/p>\n<\/li>\n<li>\n<p>\u7528\u8bcd\u7cbe\u786e<\/p>\n<\/li>\n<li>\n<p>\u683c\u5f0f\u5316<\/p>\n<p>- html \/ markdown<\/p>\n<p>- \u6307\u4ee4\u683c\u5f0f\u5316<\/p>\n<p>- \u8f93\u51fa\u5982sql \/ json \u683c\u5f0f\u5316<\/p>\n<\/li>\n<li>\n<p>COT \u601d\u7ef4\u94fe<\/p>\n<\/li>\n<li>\n<p>\u4f8b\u5b50\u8981\u591a\u6b21\u68c0\u67e5\uff0c\u786e\u4fdd\u51c6\u786e<\/p>\n<\/li>\n<li>\n<p>\u591a\u6b21\u8c03\u8bd5\uff0c\u8fed\u4ee3<\/p>\n<\/li>\n<\/ul>\n<h2>\u4ee5 Text2SQL \u4e3a\u4f8b<\/h2>\n<h3><strong>\u4efb\u52a1\u9700\u6c42<\/strong><\/h3>\n<p>\u6211\u9700\u8981\u5927\u6a21\u578b\u6839\u636e\u4e00\u6bb5\u8bdd\uff0c\u5206\u6790\u5e76\u751f\u6210 SQL \u8bed\u53e5<\/p>\n<p>\u5047\u8bbe\u67092\u5f20\u8868\uff0c\u4e00\u5f20\u4ea7\u54c1 products \u8868\uff0c\u4e00\u5f20\u9884\u5b9a bookings \u8868<\/p>\n<h3><strong>Text2SQL \u63d0\u793a\u8bcd - \u4e2d\u6587\u7248<\/strong><\/h3>\n<p>\u5bf9\u7167\u4e0a\u9762\u7684 tips \u9010\u4e00\u5206\u6790<\/p>\n<p><strong>1.\u6fc0\u6d3b\u76f8\u5173\u53c2\u6570\u548c\u8bed\u4e49\u7a7a\u95f4<\/strong><\/p>\n<p>\u4e00\u5f00\u59cb\u7ed9\u5927\u6a21\u578b\u5b9a\u4e49\u7684\u89d2\u8272\u548c\u4efb\u52a1\uff0c\u6fc0\u6d3b\u76f8\u5173\u53c2\u6570\uff0c\u5b9a\u4e49\u4e86\u5927\u6a21\u578b\u7684\u8bed\u4e49\u7a7a\u95f4\u3002\u8ba9\u5927\u6a21\u578b\u4e0d\u662f\u804a\u5929\u89e3\u95f7\uff0c\u4e0d\u662f\u5e2e\u5199\u5c0f\u8bf4\uff0c\u800c\u662f\u6709\u4e13\u4e1a\u4efb\u52a1\u7684\u201cSQL \u5f00\u53d1\u4e13\u5bb6\u201d\u3002<\/p>\n<p><strong>2.\u683c\u5f0f\u5316<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u4e86 markdown \u683c\u5f0f\u7ec4\u7ec7\u63d0\u793a\u8bcd\uff08Claude \u6a21\u578b\u7528 html \u683c\u5f0f\u6548\u679c\u66f4\u597d\uff09<\/li>\n<li>\u8f93\u51fa\u6307\u5b9a sql \u683c\u5f0f<\/li>\n<li>\u9700\u8981\u5f3a\u8c03\u7684\u7528 <strong>\u5f3a\u8c03\u5185\u5bb9<\/strong> \u52a0\u7c97\u3002<\/li>\n<\/ul>\n<p><strong>3.COT\u601d\u7ef4\u94fe<\/strong><\/p>\n<p>\u7b2c 4 \u884c\u660e\u786e\u8981\u6c42\uff1a<\/p>\n<pre><code>\u4f5c\u7b54\u524d\u5fc5\u987b\u4f7f\u7528**\u601d\u7ef4\u94fe**\u5148\u68b3\u7406\u601d\u8003\u8fc7\u7a0b\uff0c\u4f5c\u7b54\u540e\u8fd8\u9700\u5bf9\u7ed3\u679c\u8fdb\u884c\u68c0\u67e5\u3002\u3002<\/code><\/pre>\n<p><strong>4.\u793a\u4f8b<\/strong><\/p>\n<p>\u793a\u4f8b\u9700\u8981\u53cd\u590d\u68c0\u67e5\uff0c\u786e\u4fdd\u793a\u4f8b\u7b26\u5408\u81ea\u5df1\u7684\u8981\u6c42\uff0c\u9644\u4e0a\u601d\u8003\u8fc7\u7a0b\uff0c\u8f93\u51fa\u8981\u683c\u5f0f\u5316\u5e76\u4e14\u51c6\u786e<\/p>\n<p><strong>5.\u53cd\u590d\u8c03\u8bd5<\/strong><\/p>\n<p>\u89c1\u4e0b\u6587 \u201c\u8bd5\u4e00\u8bd5\u201d \u90e8\u5206<\/p>\n<p>\u5b8c\u6574\u63d0\u793a\u8bcd\uff1a<\/p>\n<pre><code>\u4f60\u662f\u4e00\u540dSQL\u5f00\u53d1\u4e13\u5bb6\u3002\u6839\u636e\u81ea\u7136\u8bed\u8a00\u9700\u6c42\uff0c\u6309\u7167\u6570\u636e\u5e93\u8868\u7ed3\u6784\u751f\u6210PostgreSQL\u67e5\u8be2\u8bed\u53e5\u3002\u4f60\u5fc5\u987b\u9075\u5faa\u4ee5\u4e0b\u64cd\u4f5c\u6307\u4ee4\u548c\u7ea6\u675f\u6761\u4ef6\u6765\u521b\u5efaPostgreSQL\u67e5\u8be2\u8bed\u53e5\u3002\n# \u64cd\u4f5c\u6307\u4ee41. \u4f5c\u7b54\u524d\u5fc5\u987b\u4f7f\u7528**\u601d\u7ef4\u94fe**\u5148\u68b3\u7406\u601d\u8003\u8fc7\u7a0b\uff0c\u4f5c\u7b54\u540e\u8fd8\u9700\u5bf9\u7ed3\u679c\u8fdb\u884c\u68c0\u67e5\u30022. **\u6838\u5fc3\u8981\u6c42**\uff1a\u751f\u6210SQL\u67e5\u8be2\u8bed\u53e5\u65f6\uff0c\u5fc5\u987b\u4fdd\u7559\u56fa\u5b9a\u7684\u57fa\u7840\u8bed\u53e5\uff0c\u4e0d\u5f97\u5bf9\u5176\u8fdb\u884c\u4efb\u4f55\u4fee\u6539\u6216\u65b0\u589e\u5185\u5bb9\uff1a     &quot;SELECT bookings.device_id, bookings.user_id FROM public.products INNER JOIN public.bookings ON public.products.device_id = bookings.device_id&quot;\n# \u7ea6\u675f\u6761\u4ef6- \u4e0d\u5f97\u865a\u6784\u4e0a\u8ff0\u8868\u7ed3\u6784\u4e2d\u4e0d\u5b58\u5728\u7684\u4efb\u4f55\u8868\u6216\u5b57\u6bb5\u3002- \u4e0d\u5f97\u5728WHERE\u5b50\u53e5\u4e2d\u4f7f\u7528bookings\u8868\u7684\u5b57\u6bb5\u3002\n# \u6570\u636e\u5e93\u8868\u7ed3\u6784\u8bf4\u660e\u8868\u540d\u4e3a\u201cproducts\u201d\uff0c\u5b57\u6bb5\u53ca\u8bf4\u660e\u5982\u4e0b\uff1a  {database_schema}\n# \u793a\u4f8b\u8f93\u5165\uff1a\u83b7\u53d6\u4ea7\u54c1\u540d\u79f0\u4e3aSSD\u4e14\u4ef7\u683c\u2265500\u7684\u9884\u8ba2\u4fe1\u606f  \u8f93\u51fa\uff1a\u8be5\u9700\u6c42\u8981\u6c42\u83b7\u53d6\u540d\u79f0\u4e3aSSD\u4e14\u4ef7\u683c\u8d85\u8fc7500\u7684\u4ea7\u54c1\u7684\u9884\u8ba2\u4fe1\u606f\u3002\u6211\u4f1a\u901a\u8fc7products\u8868\u7684price_info-&gt;&gt;&#039;price&#039;\u5b57\u6bb5\u548cstorage_info-&gt;&gt;&#039;category&#039;\u5b57\u6bb5\u8fdb\u884c\u7b5b\u9009\uff0c\u7b5b\u9009\u51fa\u4ef7\u683c\u81f3\u5c11\u4e3a500\u7684\u4ea7\u54c1\u3002\u540c\u65f6\u4fdd\u7559\u56fa\u5b9a\u7684\u57fa\u7840\u8bed\u53e5\u3002```sqlSELECT bookings.device_id,bookings.user_idFROM public.productsINNER JOIN public.bookings ON public.products.device_id = bookings.device_idWHERE (public.products.price_info-&gt;&gt;&#039;price&#039;)::numeric &gt;= 500AND (public.products.storage_info-&gt;&gt;&#039;category&#039;) = &#039;SSD&#039;```<\/code><\/pre>\n<h3><strong>\u8bd5\u4e00\u8bd5<\/strong><\/h3>\n<p>\u7528\u4e0a\u9762\u7684\u63d0\u793a\u8bcd\uff0c\u5148\u6362\u4e00\u4e2a\u7b80\u5355\u7684\u4efb\u52a1\uff0c\u770b\u662f\u5426\u6309\u7167\u9700\u8981\u7684\u8f93\u51fa\u5e76\u4e0d\u65ad\u8c03\u8bd5\uff1a<\/p>\n<p>\u91cd\u70b9\u5173\u6ce8\uff1a\u601d\u7ef4\u94fe\u601d\u8003\u8fc7\u7a0b\uff0c\u8f93\u51fa\u683c\u5f0f\u7b49<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/640-20251111175606709.png\" alt=\"\u56fe\u7247\" \/><\/p>\n<h2><strong>Text2SQL \u63d0\u793a\u8bcd - \u82f1\u6587\u7248<\/strong><\/h2>\n<p>\u5982\u4e0b\uff1a<\/p>\n<pre><code>You are an expert SQL developer. Given a natural language requirement, generate a PostgreSQL query as per database Schema. You must follow the instructions and constraints below to create the PostgresQL query.\n# INSTRUCTIONS:- 1. ALWAYS take chain of thought before answering and review the result after answersing.- 2. **Core Requirement**: When generating the SQL query, you **must retain the fixed base statement** without any modifications or additions:     &quot;SELECT bookings.device_id, bookings.user_id FROM public.products INNER JOIN public.bookings ON public.products.device_id = bookings.device_id&quot;\n# CONSTRAINTS- DO NOT make up any table and field which is not in the above Schema.- DO NOT use bookings table fields in WHERE clause.\n# Here are the database table Schema:Table name is &quot;products&quot;, fields and description are as follows:{database_schema}\n# EXAMPLEinput: Give me the booking information of products which name is SSD and price &gt;= 500output: The requirement ask for NVMe SSD and price more than 500. I will filter the products table based on the price_info-&gt;&gt;&#039;price&#039; and storage_info-&gt;&gt;&#039;category&#039; fields to get products with at least 500. And keep the fixed base statement.```sqlSELECT bookings.device_id,bookings.user_id,FROM public.productsINNER JOIN public.bookings ON public.products.device_id = bookings.device_idWHERE (public.products.price_info-&gt;&gt;&#039;price&#039;)::numeric &gt;= 500AND (public.products.storage_info-&gt;&gt;&#039;category&#039;) = &#039;SSD&#039;```<\/code><\/pre>\n<blockquote>\n<p>\u5434\u6069\u8fbe\u63d0\u793a\u8bcd\u8bfe\u7a0b: <a href=\"https:\/\/learn.deeplearning.ai\/courses\/chatgpt-prompt-eng\">https:\/\/learn.deeplearning.ai\/courses\/chatgpt-prompt-eng<\/a><\/p>\n<p>\u89c6\u89c9\u6a21\u578b\u63d0\u793a\u8bcd: <a href=\"https:\/\/learn.deeplearning.ai\/courses\/prompt-engineering-for-vision-models\">https:\/\/learn.deeplearning.ai\/courses\/prompt-engineering-for-vision-models<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u63d0\u793a\u8bcd\u662f\u4eba\u4e0e\u5927\u6a21\u578b\u4e4b\u95f4\u6c9f\u901a\u7684\u8bed\u8a00\uff0c\u5176\u91cd\u8981\u6027\u4e0d\u8a00\u800c\u55bb\uff0c\u5199\u4e86\u5f88\u591a\u63d0\u793a\u8bcd\u4e5f\u8e29\u4e86\u4e0d\u5c11\u5751\uff0c\u8ddf\u5927\u5bb6\u5206\u4eab\u4e00\u4e9b\u5fc3\u5f97\uff0c\u672c\u6587\u4ee5 Text2SQL \u4e3a\u4f8b\uff0c\u8be6\u7ec6   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[288],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9500"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9500"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9500\/revisions"}],"predecessor-version":[{"id":9501,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9500\/revisions\/9501"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9500"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}