简体   繁体   English

有没有像sqlite3“函数”这样的东西?

[英]Is there such a thing as an sqlite3 “function”?

Suppose I have a long query in sqlite3 command line as follows:假设我在 sqlite3 命令行中有一个长查询,如下所示:

select col1, col2, col3 from table1
join table2
on table2.table1_id=table1.id
join table3
on table3.table2_id=table_2.id
where col1 like "%some text%";

Now I would like to try this query for different wildcards like "%some text 2%" and "%some text 3%" .现在,我想针对不同的通配符尝试此查询,例如"%some text 2%""%some text 3%"

I only know of two options here:我只知道这里有两个选项:

  • rewriting the query multiple times in the command line (a pain in the butt)在命令行中多次重写查询(屁股痛)
  • committing the whole thing to a script and executing with .read test.sql将整个事情提交到脚本并使用.read test.sql执行

Is there another way?还有其他方法吗? Does there exist some function-like concept in sqlite3 which applies here? sqlite3 中是否存在一些类似函数的概念适用于这里? Ideally I would like to define the "function" and call it from the command line.理想情况下,我想定义“函数”并从命令行调用它。

SQLite does not support functions. SQLite 不支持函数。

What you could do, to save you from retyping the main part of the query, is create a view:为了避免重新键入查询的主要部分,您可以做的是创建一个视图:

create view MyView as
select col1, col2, col3 from table1
join table2
on table2.table1_id=table1.id
join table3
on table3.table2_id=table_2.id

Then you can add the WHERE clause whenever you want to execute it:然后你可以在你想要执行它时添加WHERE子句:

SELECT * FROM MyView
WHERE col1 LIKE ?; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM