首页 > 解决方案 > 使用可选查询参数构造 SQL 查询

问题描述

我正在尝试针对来自端点的可选查询参数构建一个 sql 查询。

例如我有一个网址 http://localhost/api?one=1

现在形成上面的 url,我需要构建类似 .. 的查询

SELECT DISTINCT tb_one.id as id, tb_one.title as title from tb_one 
  JOIN
  tb_one ON tb_main.id = tb_one.id
  where other_id in (1) and tb_one.id in (2) 

还有例如我有一个网址 http://localhost/api?one=1&two=1

SELECT DISTINCT tb_one.id as id, tb_one.title as title from tb_one 
      JOIN
      tb_one ON tb_main.id = tb_one.id 
      JOIN
      tb_two ON tb_main.id = tb_wo.id
      where other_id in (1) 

它可以是具有不同组合的 n 个参数。

我们可以在 Go 中构造这样的查询吗?

标签: gogo-gingo-sqlite3

解决方案


推荐阅读